• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • 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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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