C# 5.0 in a Nutshell by Joseph Albahari

C# 5.0 in a Nutshell by Joseph Albahari

Author:Joseph Albahari [Joseph Albahari and Ben Albahari]
Language: eng
Format: epub
Tags: COMPUTERS / Programming Languages / C#
ISBN: 9781449340964
Publisher: O'Reilly Media
Published: 2012-06-19T16:00:00+00:00


Note

You can track a task’s execution status via its Status property.

Wait

Calling Wait on a task blocks until it completes and is the equivalent of calling Join on a thread:

Task task = Task.Run (() => { Thread.Sleep (2000); Console.WriteLine ("Foo"); }); Console.WriteLine (task.IsCompleted); // False task.Wait(); // Blocks until task is complete

Wait lets you optionally specify a timeout and a cancellation token to end the wait early (see Cancellation).

Long-running tasks

By default, the CLR runs tasks on pooled threads, which is ideal for short-running compute-bound work. For longer-running and blocking operations (such as our example above), you can prevent use of a pooled thread as follows:

Task task = Task.Factory.StartNew (() => ..., TaskCreationOptions.LongRunning);



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.