Reputation: 1079
I have limited experience designing multi-threaded applications, and was hoping for some general advice on the following simple problem:
An application communicates with a collection of network devices. At regular intervals, and on demand, it must perform a maintenance task on each device. Previously it had done this by calling a method on each device sequentially. However this is now taking too long, so it must be modified to call the method on each device asynchronously.
My initial thoughts are that it needs to call the method for each device asynchronously, have a callback set a variable which indicates that the method is complete, and in the meantime loop through these variables until they all indicate that their corresponding method is complete, so the overall process can return as complete.
FYI I am programming in C#. I have high level knowledge but little experience of ThreadStart, ThreadPool, etc.
Upvotes: 0
Views: 158
Reputation: 182761
You can use a job count, job lock, and shutdown flag. To shut down:
To assign a job:
When finished with job:
Upvotes: 0
Reputation: 3689
Without digging too much into how threading affects your code and how does it behave under the hood on single vs multi-processor machines, you have several alternatives:
1) The classic Thread class, which accepts a delegate to perform its duty. Preferred for lengthy tasks that don't necessarily require status updates.
2) You can use BackgroundWorkers, which expose state events. They use the .NET ThreadPool
3) You can invoke your delegates by your own with BeginInvoke, which will run its task on a .NET ThreadPool thread and allow you to specify a callback to be run upon task completion
4) You can use the new & nice Task Parallel Library, provides you everything you need in a very elegant fashion
Remember that if those tasks you wish to execute "in parallel" share any state, you must serialize access to that using the lock statement (other methods exists, depending on the requirements). Beware of deadlocks!
Upvotes: 1
Reputation: 27495
If you are using .NET 4.0, you might want to check these out:
General intro to parallel programming
Upvotes: 2