pause a thread, let other threads run


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: pause a thread, let other threads run

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    pause a thread, let other threads run

    Hi there. In my application I have a loop that makes calls to a website to download data. I'm using a DownloadDataAsynch call with a handler.

    I'm making many such calls in a loop. What I'd like to do is, if I have more than a certain number of replies pending, I should pause the loop and let the data handlers run.

    How do I do that?

    Code:
         Dim DataReader As SqlDataReader = sp_call.ExecuteReader
         Dim count As Integer = 0
         
         '---------------------------
         ' loop through results  
         ' get description for 
         ' each item on the list
         '---------------------------
         While DataReader.Read()
                ' get the item ID
                Dim itemID As String = DataReader(0).ToString
                Dim request_string As String
    
                'get the data for that item
                request_string =   [  web request constructed using item no ] 
    
                ' create an asynchronous call
                Dim data_downloader As New WebClient
                AddHandler data_downloader.DownloadDataCompleted, AddressOf Data_Downloader_complete
                data_downloader.DownloadDataAsync(New Uri(request_string), count)
                items_pending = items_pending + 1
                   
                If items_pending > 50 Then
                     [ pause until items_pending < 50 ]
                End If
    
          End While

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    It does not look like you are actually using threads here.

    Do the handlers reduce the count in the 'items_pending' variable?
    If they do then create a loop that waits for the count to get below 50
    Code:
      Do until items_pending < 50
        Application.DoEvents()
      Loop
    Now instead of a loop were you to use the Thread.Sleep() method And your handlers are in the same thread then they would sleep as well.
    example:
    System.Threading.Thread.Sleep(amount of time to sleep)

    One More Thing I noticed that you assigned 'itemID' within the Dim Statement.
    You should know that 'itemId' is only initialized once, reguardless how many loops you go through. A Dim statement only initializes once. If you want to assign 'itemId' each time the loop iterates then you must seperate the assignment from the declare statement. Like This:
    Code:
      Dim itemID As String 
      itemID = DataReader(0).ToString

  3. #3
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Ron, assigning a value on the same line as the declaration gives exactly the same result as separating them as you did.

    The Dim itself is not compiled, it is only an indication to the compiler to reserve a pointer. Only the assignment ends up in the resulting MSIL code.

    You can see this very clearly when debugging. The debugger highlight the assignment but not the Dim. And it does it in each iteration of the loop.

    Try the following, and you will see that the string keeps growing on each turn:

    Code:
    While True
    	Dim s As String = s & "."
    End While
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    I could have sworn I got nailed by this before. That's why I pointed it out...????

  5. #5
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    This is often the price to pay for experience. I often fall "I could have sworn I got nailed by this before". Experienced programmers who have worked in many languages and environments are sometimes mixed up when confronting their old memories.

    Might also be one of those little things that have changed after the first betas and even the first release of Visual Basic in 2002. There are a few of those. I still see programmers that always Flush a file before closing it or thing that Application.Run (New Form) does not display the form as a dialog. Those are little things that have changed since the release of the first versions of the framework.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  6. #6
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Quote Originally Posted by JBourgeois View Post
    This is often the price to pay for experience. I often fall "I could have sworn I got nailed by this before". Experienced programmers who have worked in many languages and environments are sometimes mixed up when confronting their old memories.

    Might also be one of those little things that have changed after the first betas and even the first release of Visual Basic in 2002. There are a few of those. I still see programmers that always Flush a file before closing it or thing that Application.Run (New Form) does not display the form as a dialog. Those are little things that have changed since the release of the first versions of the framework.
    Old memories? I don't think so. But I have only been using VB 2005 Express. I did not jump into VB.Net right away, I stuck with VB6 until I had a need for the .Net Framework and it was only last year when I really got into it. But with Microsof things can change alot in a year. Oh well, so long as it works, that's the important part.

Similar Threads

  1. How do i run 2 threads at once
    By AdRock in forum Java
    Replies: 3
    Last Post: 04-21-2007, 01:26 PM
  2. Tree structure and threads in VB.NET
    By ozkanozlu in forum .NET
    Replies: 6
    Last Post: 02-23-2007, 09:30 AM
  3. Replies: 2
    Last Post: 05-30-2005, 06:10 AM
  4. thread
    By il7omed in forum Java
    Replies: 1
    Last Post: 05-24-2005, 05:18 AM
  5. C# and ThreadPools
    By Erik Funkenbusch in forum .NET
    Replies: 6
    Last Post: 10-18-2001, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center