Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Happens To a Task When It's Cancelled?

Tags:

c#

.net

I was toying with Await/Async and CancellationTokens. My code works, but what happens to the Task when it's Cancelled? Is it still taking up resources or is it garbage collected or what?

Here is my code:

    private CancellationTokenSource _token = new CancellationTokenSource();

    public Form1()
    {
        InitializeComponent();
    }

    async Task<String> methodOne()
    {
        txtLog.AppendText("Pausing for 10 Seconds \n");
        var task = Task.Delay(10000, _token.Token);
        await task;
        return "HTML Returned. \n";

    }

    private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
            var task1 = methodOne();
            await task1;
            txtLog.AppendText(task1.Result + "\n");
            txtLog.AppendText("All Done \n");
        }
        catch (OperationCanceledException oce)
        {
            txtLog.AppendText("Operation was cancelled");
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _token.Cancel();
    }
like image 987
James Jeffery Avatar asked Aug 21 '13 11:08

James Jeffery


People also ask

How do you cancel a task?

In the Task classes, cancellation involves cooperation between the user delegate, which represents a cancelable operation and the code that requested the cancellation. A successful cancellation involves the requesting code calling the CancellationTokenSource.Cancel method, and the user delegate terminating...

What is task cancellation in C #?

Task cancellation in C# and things you should know about it. The task mechanism in C# is a powerful beast in the area of parallel and concurrent programming. Controlling this beast may take lots of effort and pain. .NET Framework 4 introduces a convenient approach for cancellation of asynchronous operations.

What happens when a task is canceled in Salesforce?

A task that is canceled in this way transitions to the Canceled state, which the calling code can use to verify that the task responded to its cancellation request. The following example shows the basic pattern for task cancellation that throws the exception.

Why do I get taskcanceledexception while waiting?

If you are waiting on a Task that transitions to the Canceled state, a System.Threading.Tasks.TaskCanceledException exception (wrapped in an AggregateException exception) is thrown. Note that this exception indicates successful cancellation instead of a faulty situation.


1 Answers

When a task is cancelled, it completes (in the cancelled state). It acts just like any other object in regards to garbage collection: if you have no references to it, it will be collected.

Note that although Task does implement IDisposable, you don't have to dispose it unless you're using the IAsyncResult.AsyncWaitHandle member.

like image 154
Stephen Cleary Avatar answered Oct 13 '22 08:10

Stephen Cleary