views:

156

answers:

4

I want to get a good grasp of multi-threading in c#. I've read some articles that explain the concepts like Joseph Albahari's tutorials, but as you know no matter how much you read, most of it becomes rubbish if you don't practice. I need something that has instructive and pragmatic code examples related to real life practices, not some examples that print some lines. Do you have any suggestions?

+2  A: 

If you're doing any winforms or wpf development, you'll quickly run across issues when you try to do "stuff" in the UI thread.

Let's say that you need to read and parse the contents of a large (2GB) XML file. If the work were performed in the UI thread, the interface would hang until the work had been completed. Conversely, if you were to do the work correctly in a worker thread, then you could keep the UI responsive via messaging and let the user know what you're currently doing (status bar (ugh,) or display in text what you're doing "Reading XML.", etc.)

A good simple example would be to make a sample application and have it fire off a BackgroundWorker to handle some arbitrary work in the background (it could even be Thread.Sleep(10000), or something trivial like that.)

I'd say this is one of the many good starting points out there on the subject.

http://msdn.microsoft.com/en-us/library/cc221403%28VS.95%29.aspx

Ian P
I am in the background communication area mostly, with sockets and web services. but thanks for the suggestion, i'll start with it :)
davsan
+2  A: 

This site has a few sample applications that I think would be decent practice applications to implement. However, it seems like the links to the source code are broken. Nonetheless, I believe the applications presented represent very practical examples. A few include:

  • Desktop Search
  • Download Manager
  • FTP Client
  • File Compression
  • Multiple RSS Feeds
Garett
yeah this was what i was looking for. too bad the sources are gone :(
davsan
+2  A: 

Some kind of random number-crunching is a good test for this. I taught myself threading by writing a prime number finder, then breaking my "search" numbers into blocks and using a thread to work through each one.

This let me set some variables on block size, number of threads to use, wait time between firing threads etc. to test how each of these affects performance.

ck
+1  A: 

guys guys I think I found a good site: planet-source-code.com. Searching in .Net codes with "thread" keyword seems to return some good examples, like

  • multi threaded folder synchronization
  • multi threaded TCP server
  • background file downloader
  • async. socket
  • P2P file sharing
  • simple POP3 console mail checker and lots of others!

yay!

davsan