views:

49

answers:

1

http://msdn.microsoft.com/en-us/library/dd988458.aspx

UPD:

so, let's discuss this article then: http://msdn.microsoft.com/en-us/library/dd997396.aspx

I've changed that code a little: static void Main() {

        var tokenSource2 = new CancellationTokenSource();
        CancellationToken ct = tokenSource2.Token;

        var task = Task.Factory.StartNew(() =>
        {

            // Were we already canceled?
            ct.ThrowIfCancellationRequested();

            bool moreToDo = true;
            Thread.Sleep(5000);
            while (moreToDo)
            {

                // Poll on this property if you have to do
                // other cleanup before throwing.
                if (ct.IsCancellationRequested)
                {
                    Console.WriteLine("exit");
                    // Clean up here, then...
                    ct.ThrowIfCancellationRequested();
                }

            }
        }, tokenSource2.Token); // this parameter useless

        Console.WriteLine("sleep");
        Thread.Sleep(2000);
        Console.WriteLine("cancel");

        tokenSource2.Cancel();

        // Just continue on this thread, or Wait/WaitAll with try-catch:
        try
        {
            task.Wait();
        }
        catch (AggregateException e)
        {
            foreach (var v in e.InnerExceptions)
            {
                Console.WriteLine(e.Message + " " + v.Message);
            }
        }

        Console.ReadKey();
    }

UPD: Well, this changes only task.IsCanceled, which is imho useless, due to I still ought to implement all manually.

+2  A: 

Cancellation with Tasks is still cooperative. You wouldn't want a thread to be killed in the middle of some critical operation. You need to check for it.

CancellationTokens are better than simpler constructs like a ManualResetEvent for signalling shutdown of an operation because you can cascade or combine them, for example, you can have one for overall application shutdown and you can combine it with one for canceling a particular task. The task only has to look at the one CancellationToken but you can cancel it from either CancellationTokenSource.

Hightechrider
so, how can I get that token argument in the Work()?
zerkms
Well, you can pass it in using http://msdn.microsoft.com/en-us/library/dd780315.aspx but the REAL utility of the cancellation tokens comes into play when you start implementing your own `TaskFactory` and want to have cascading cancellation tokens and such like and then it's far more powerful than a simple `ManualResetEvent` or similar.
Hightechrider
@zerkms: Also read [cancellation](http://msdn.microsoft.com/en-us/library/dd997364.aspx) on MSDN which gives more of a high-level view of the unified cancellation framework. Task cancellation is just how `Task` objects use cancellation. If you read the "big picture" cancellation article, then the benefits should become clearer.
Stephen Cleary
ok, thanks. i will read it.
zerkms