15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

summary ❘ 543<br />

comp.LongTaskAsync("input", 33);<br />

}<br />

Console.ReadLine();<br />

static void Comp_LongTaskCompleted(object sender,<br />

LongTaskCompletedEventArgs e)<br />

{<br />

Console.WriteLine("completed, result: {0}, thread: {1}", e.Output,<br />

Thread.CurrentThread.ManagedThreadId);<br />

}<br />

code snippet AsyncComponent/Program.cs<br />

With a Windows Forms application the SynchronizationContext is set to<br />

WindowsFormsSynchronizationContext — that’s why the event h<strong>and</strong>ler code is invoked in the<br />

same thread:<br />

WindowsFormsSynchronizationContext syncContext =<br />

new WindowsFormsSynchronizationContext();<br />

SynchronizationContext.SetSynchronizationContext(syncContext);<br />

summary<br />

This chapter explored how to code applications that use multiple threads using the System.Threading<br />

namespace <strong>and</strong> multiple tasks using the System.Threading.Tasks namespace. Using multithreading in<br />

your applications takes careful planning. Too many threads can cause resource issues, <strong>and</strong> not enough<br />

threads can cause your application to seem sluggish <strong>and</strong> to perform poorly. With tasks, you get an<br />

abstraction to threads. This abstraction helps you not to create too many threads because threads are<br />

reused from a pool.<br />

You’ve seen various ways to create multiple threads such as using the delegate, timers, a ThreadPool, <strong>and</strong><br />

the Thread class. Various synchronization techniques have been explored, such as a simple lock statement,<br />

as well as the Monitor, Semaphore, <strong>and</strong> Event classes. You’ve seen how to program the asynchronous<br />

pattern with the IAsyncResult interface <strong>and</strong> the event-based asynchronous pattern.<br />

The System.Threading namespace in the .<strong>NET</strong> Framework gives you multiple ways to manipulate threads;<br />

however, this does not mean that the .<strong>NET</strong> Framework h<strong>and</strong>les all the difficult tasks of multithreading<br />

for you. You need to consider thread priority <strong>and</strong> synchronization issues. This chapter discussed these<br />

issues <strong>and</strong> how to code for them in your <strong>C#</strong> applications. It also looked at the problems associated with<br />

deadlocks <strong>and</strong> race conditions.<br />

Just remember that if you are going to use multithreading in your <strong>C#</strong> applications, careful planning needs to<br />

be a major part of your efforts.<br />

Here are some final guidelines regarding threading:<br />

➤<br />

➤<br />

➤<br />

Try to keep synchronization requirements to a minimum. Synchronization is complex <strong>and</strong> blocks<br />

threads. You can avoid it if you try to avoid sharing state. Of course, this is not always possible.<br />

Static members of a class should be thread-safe. Usually, this is the case with classes in the .<strong>NET</strong><br />

Framework.<br />

Instance state does not need to be thread-safe. For best performance, synchronization is better used<br />

outside of the class where it is needed, <strong>and</strong> not with every member of the class. Instance members of<br />

.<strong>NET</strong> Framework classes usually are not thread-safe. In the MSDN library, you can find this information<br />

documented for every class of the Framework in the Thread Safety section.<br />

The next chapter gives information on another core .<strong>NET</strong> topic: security.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!