[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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.
0
talker2004
Asked:
talker2004
  • 3
  • 3
1 Solution
 
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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
 
talker2004Author Commented:
This is so what i wanted to do and easy to test.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now