Reputation: 122222
I have a basic cs-major understanding of multi-threading but have never had to do anything beyond simple timers in an application. Does anyone know of a good resource that will give me a tour how to work with multi-threaded applications, explaining the basics and maybe posing some of the more difficult stuff?
Upvotes: 7
Views: 1265
Reputation: 416149
There are 4 basic ways to synchronize threads in .Net:
Generally you want to start at the top of that list and work down. That means first look and see if a backgroundworker control is appropriate to the situation. However, it pretty much assumes windows forms and that you're only spawning one new thread.
So next try waithandles. Waithandles are good for coordinating several threads together. You can kick them all off and wait for them all to finish, or if you want to keep a certain number active you keep waiting for just one and spawning the next when it finishes. Or maybe you know one thread will finish much sooner, so you can wait for it to finish, do a little bit of work, and then wait for the rest to finish.
Waithandles might seem like a bit much if, say, you're only spawning one additional thread and you don't want to block until it's finished. Then you might use a callback, so that the function you designate will be called as soon as the thread completes.
Finally, if and only if for some reason none of the above will work you can fall back to polling.
I can think of 5 different ways to get a new thread in .Net, also roughly in order:
Upvotes: 5
Reputation: 52198
This is a great free resource by Joseph Albahari. Threading in C#
Upvotes: 23
Reputation: 30819
One of the best resources I know on the subject is the "threading in C#" book: http://www.albahari.com/threading/
I has a great overview of all a .net developer need to understand in order to program multi threaded applications.
Upvotes: 1
Reputation: 58969
Two great articles:
What Every Dev Must Know About Multithreaded Apps
Understand the Impact of Low-Lock Techniques in Multithreaded Apps
Although this article isn't exactly what you are looking for specifically, it will hopefully be of assistance generally (i.e. it is related, and a very good read):
The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software
Upvotes: 11