03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

146 ❘ CHAPTER 6 Methods<br />

Next, the code creates a Task object for the method represented by the countDelegate variable.<br />

The Task constructor takes as parameters the delegate and the value 5 that it should pass to the<br />

delegate’s method.<br />

The code then calls the Task’s Start method to start the task running on a new thread.<br />

The program then performs its own count on the UI thread. When it finishes it displays a message<br />

in the Label and re-enables the program’s Button.<br />

Having finished its work, the code uses the await keyword to wait for the Task to finish and saves<br />

the result returned in the variable result.<br />

The code finishes by displaying the result it received from the asynchronous thread.<br />

The following code shows the new version of the Count method.<br />

// Count pausing 1 second between numbers.<br />

// Return the final value displayed.<br />

private int Count(object maxObj)<br />

{<br />

int max = (int)maxObj;<br />

for (int i = 1; i

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

Saved successfully!

Ooh no, something went wrong!