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

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

talker2004Author Commented:

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!
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

talker2004Author Commented:
thanks, it makes sense now. I have loops going on just as you have described.

I am really excited to try it out
I just gave the following a quick run through to see if it works and it does!
I guess you could find something better to do during the loops...
Sub Main()
	Dim Threads As New List(Of Threading.Thread)
	For I As Integer = 1 To 20
		Console.WriteLine("Starting thread {0}", I)
		Dim NewThread As New Threading.Thread(AddressOf Wait)
		NewThread.Name = I
		NewThread.Start(I * 1000)
	Dim Start = Now
	For Each Thread In Threads
		Console.WriteLine("Waiting on thread {0}", Thread.Name)
		While Thread.IsAlive
		End While
		Console.WriteLine("Thread died after {0}", (Now - Start).ToString)
End Sub
Sub Wait(ByVal Time As Integer)
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
talker2004Author Commented:
This is so what i wanted to do and easy to test.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.