Link to home
Start Free TrialLog in
Avatar of talker2004
talker2004Flag for United States of America

asked on

C# / VB.NET - Apply Multithreading techniques to a slow algorithm

Hi have a very large algorithm which can become slow in some extreme circumstances. I want to add some threads to try to speed up the processing. Here is an example of how i am doing it.

 for each CustomerID in Customers
    ReallyLongProcess(CustomerID)
next

DisplayResults
 
If someone could give me a vb.net or c# example of calling the ReallyLongProcess method asynchronously each time and then an example to determine when all the callbacks are finished I would greatly appreciate it.
Avatar of BToson
BToson
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,
How about something like one of the following.  The second option utilises the CLR thread pool.
Dim NewThread As New Threading.Thread(AddressOf ReallyLongProcess)
NewThread.Start(CustomerID)
 
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf ReallyLongProcess), CustomerID)

Open in new window

Avatar of talker2004

ASKER


This looks good

One thing i am still not clear on is how to handle the callback to execute the DisplayResults method. I see that you are specifying the wait callback but i am unclear how to tie in the event handler.
The only way I can think of at the moment is using the first method, storing them in a List(Of Thread), iterating through them on a While loop and checking the IsAlive property.
It won't be the cleanest way of doing it but I guess it should work!
thanks, it makes sense now. I have loops going on just as you have described.

I am really excited to try it out
ASKER CERTIFIED SOLUTION
Avatar of BToson
BToson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is so what i wanted to do and easy to test.