9

When canceling a task that has a timeout (before the timeout has ended) using a cancel token an exception is thrown. Example:

mytask.start();
bool didTaskRunInTime = mytask.wait(5 mins, _cancelToken);

Which means I cannot go on like below.

//was the task cancelled
if (_cancelToken.IsCancelRequested)
{
    // log cancel from user to file etc
}

if (didTaskRunInTime )
{
    int taskResult = myTask.Result;
    // log result to file
}
else if (!_cancelToken.IsCancelRequested)
{
    // Tell user task timed out , log a message etc
}

I will have to do all this in my catch block and my code is looking messy. What is the correct way to do this?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Gullu
  • 3,477
  • 7
  • 43
  • 70

2 Answers2

18

You could call Task.WaitAny with an array of just that task. Then you can act on the status of the task, however the method returns. Sample code:

using System;
using System.Threading;
using System.Threading.Tasks;

class Test
{
    static void Main()
    {
        Task sleeper = Task.Factory.StartNew(() => Thread.Sleep(100000));

        int index = Task.WaitAny(new[] { sleeper },
                                 TimeSpan.FromSeconds(0.5));
        Console.WriteLine(index); // Prints -1, timeout

        var cts = new CancellationTokenSource();

        // Just a simple wait of getting a cancellable task
        Task cancellable = sleeper.ContinueWith(ignored => {}, cts.Token);

        // It doesn't matter that we cancel before the wait
        cts.Cancel();

        index = Task.WaitAny(new[] { cancellable },
                             TimeSpan.FromSeconds(0.5));
        Console.WriteLine(index); // 0 - task 0  has completed (ish :)
        Console.WriteLine(cancellable.Status); // Cancelled
    }
}

Note that if the task is faulted, you should "observe" the exception in order to avoid it going bang when it's finalized :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I think it will throw, WaitAny will aggregate all exceptions, you should even call wait after the waitAny (and maybe cancelling the others) in order to catch their exceptions. – eFloh Jul 13 '11 at 16:28
  • thanks John. But waitany does not take a timeout. I have long running export that needs timeout handling. – Gullu Jul 13 '11 at 16:30
  • 1
    @eFloh: `WaitAll` aggregates exceptions. `WaitAny` doesn't. See the sample code. – Jon Skeet Jul 13 '11 at 16:36
  • @Gullu: Yes it does (or at least can) - see my sample code :) – Jon Skeet Jul 13 '11 at 16:36
  • @Jon Skeet: good to know, that means infact that after using waitAny, one has to ensure that alle other tasks' exceptions will be caught (e.g. using WaitAll) – eFloh Jul 13 '11 at 16:47
  • @eFloh: Indeed. I believe the need to observe exceptions may be changing in .NET 5 though... – Jon Skeet Jul 13 '11 at 16:50
  • thanks Jon. Solution for sure. But now handling the exception is looking more readable and easy to maintain :-) Like Vlad has shown below. – Gullu Jul 13 '11 at 17:27
3

try to use OperationCanceledException

try
{           
    mytask.start();
    bool didTaskRunInTime = mytask.wait(5 mins, _cancelToken);

    if (didTaskRunInTime )
    {
        int taskResult = myTask.Result;
        //log result to file
    }
    else
    {
        // Tell user task timed out , log a message etc
    }
}
catch (OperationCanceledException ex)
{
    // log cancel from user to file et
}
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
  • that was what i was trying to avoid but I may have no choice. My reasoning was that if someone set a cancelToken and intends to cancel why the hell is this thing designed to throw an exception. thanks Vlad – Gullu Jul 13 '11 at 16:34
  • There's no requirement to use exceptions here - see my answer for an example without a try/catch at all. – Jon Skeet Jul 13 '11 at 16:37