Saturday, February 19, 2011

Parallel Programming in .NET 4.0 Cancelling Tasks PART-III.

In this Blog post we are going to see how we can cancel the Task which is already running. Here is the steps we need to perform to cancelling the running Task.

1) First  create the   image .

2) Get the CancellationToken from CancellationTokenSource  Token Property.

3) Pass the CancellationToken  Token when we creating the Task or Task<T>.

image

Now to cancel the running Task simply call the  Cancel() method on image class variable instance.

For a better implementation of cancellation, the task itself can regularly poll the token by calling token.ThrowIfCancellationRequested (), so that the task will be canceled even if it has already started running by the time the token was canceled.

Here is an Action

Cancellation by Polling

image

 

Registering the Cancellation Delegate

You can register the callback method when Task Cancellation is requested. This type of Cancellation very useful in UI application .

image

Nandri(Thanks)

Sreenivasaragavan

No comments: