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.

The Thread Class ❘ 495<br />

static void TakesAWhileCompleted(IAsyncResult ar)<br />

{<br />

if (ar == null) throw new ArgumentNullException("ar");<br />

TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;<br />

Trace.Assert(d1 != null, "Invalid object type");<br />

}<br />

int result = d1.EndInvoke(ar);<br />

Console.WriteLine("result: {0}", result);<br />

With a callback method, you need to pay attention to the fact that this method is<br />

invoked from the thread of the delegate <strong>and</strong> not from the main thread.<br />

Instead of defi ning a separate method <strong>and</strong> passing it to the BeginInvoke() method, Lambda expressions can be<br />

used. The parameter ar is of type IAsyncResult . With the implementation, there is no need to assign a value to<br />

the last parameter of the BeginInvoke() method because the Lambda expression can directly access variable d1<br />

that is in the outer scope. However, the implementation block of the Lambda expression is still invoked from the<br />

thread of the delegate, which might not be clear immediately when defi ning the method this way.<br />

static void Main()<br />

{<br />

TakesAWhileDelegate d1 = TakesAWhile;<br />

d1.BeginInvoke(1, 3000,<br />

ar =><br />

{<br />

int result = d1.EndInvoke(ar);<br />

Console.WriteLine(“result: {0}”, result);<br />

},<br />

null);<br />

for (int i = 0; i < 100; i++)<br />

{<br />

Console.Write(“.”);<br />

Thread.Sleep(50);<br />

}<br />

}<br />

The programming model <strong>and</strong> all of these options with asynchronous delegates — polling, wait<br />

h<strong>and</strong>les, <strong>and</strong> asynchronous callbacks — are not only available with delegates. The same programming<br />

model — this is the asynchronous pattern — can be found in various places in the .<strong>NET</strong> Framework.<br />

For example, you can send an HTTP Web request asynchronously with the BeginGetResponse()<br />

method of the HttpWebRequest class. You can send an asynchronous request to the database with<br />

the BeginExecuteReader() of the SqlComm<strong>and</strong> class. The parameters are similar to those of the<br />

BeginInvoke() class of the delegate, <strong>and</strong> you can use the same mechanisms to get the result.<br />

HttpWebRequest is covered in Chapter 24, “ Networking, ” <strong>and</strong> SqlComm<strong>and</strong> is<br />

discussed in Chapter 30, “ Core ADO.<strong>NET</strong>.”<br />

Instead of using the delegate for creating threads, you can create threads with the Thread class, which is<br />

covered in the next section.<br />

The Thread Class<br />

With the Thread class, you can create <strong>and</strong> control threads. The code here is a very simple example of<br />

creating <strong>and</strong> starting a new thread. The constructor of the Thread class is overloaded to accept a delegate<br />

parameter of type ThreadStart or ParameterizedThreadStart . The ThreadStart delegate defi nes a<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!