Summary


This chapter took a quick look at how to code applications that utilize multiple threads using the System.Threading namespace. Using multithreading in your applications takes careful planning. Too many threads can cause resource issues, and not enough threads can cause your application to seem sluggish and to perform poorly.

The System.Threading namespace in the .NET Framework does allow you to manipulate threads; however, this does not mean that the .NET Framework handles all the difficult tasks of multithreading for you. You have to consider thread priority and synchronization issues. This chapter discussed these issues and how to code for them in your C# applications. It also looked at the problems associated with deadlocks and race conditions.

Just remember that if you are going to use multithreading in your C# applications, careful planning should be a big part of your efforts.

Some final guidelines regarding threading:

  • Try to keep synchronization requirements to a minimum. Synchronization is complex and blocks threads. You can avoid it if you try to avoid sharing state. Of course, this is not always possible.

  • Static members of a class should be thread-safe. Usually, this is the case with classes in the .NET Framework.

  • Instance state does not need to be thread-safe. For best performance synchronization is better used outside of the class where it is needed and not with every member of the class. Instance members of .NET Framework classes usually are not thread-safe. You can find this information documented for every class of the Framework in the Thread Safety section.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net