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

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

x
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.

BTosonCommented:
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

0
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.
0
BTosonCommented:
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!
0
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
0
BTosonCommented:
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)
		Threads.Add(NewThread)
	Next
	Dim Start = Now
	For Each Thread In Threads
		Console.WriteLine("Waiting on thread {0}", Thread.Name)
		While Thread.IsAlive
			Threading.Thread.Sleep(100)
		End While
		Console.WriteLine("Thread died after {0}", (Now - Start).ToString)
	Next
End Sub
Sub Wait(ByVal Time As Integer)
	Threading.Thread.Sleep(Time)
End Sub

Open in new window

0

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.
0
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.