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?
Dim DataReader As SqlDataReader = sp_call.ExecuteReader
Dim count As Integer = 0
' loop through results
' get description for
' each item on the list
' 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 ]
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
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.
Do until items_pending < 50
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:
Dim itemID As String
itemID = DataReader(0).ToString
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:
Dim s As String = s & "."
I could have sworn I got nailed by this before. That's why I pointed it out...????
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.
Originally Posted by JBourgeois
Last Post: 04-21-2007, 02:26 PM
By ozkanozlu in forum .NET
Last Post: 02-23-2007, 10:30 AM
By chill_out in forum C++
Last Post: 05-30-2005, 07:10 AM
Last Post: 05-24-2005, 06:18 AM
By Erik Funkenbusch in forum .NET
Last Post: 10-18-2001, 04:43 AM
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL