Home > Back-end >  Waiting for single event with timeout
Waiting for single event with timeout

Time:01-19

My current solution requires a class global CancellationTokenSource. It is possible to do the same without it?

CancellationTokenSource _waitCTS = null;

private async Task WaitWithTimeout(string param)
{
    _waitCTS?.Dispose();
    _waitCTS = new CancellationTokenSource();

    XClass x = new XClass(param);
    x.OnReceived  = X_OnReceived;

    try
    {
        await Task.Delay(5000, _waitCTS.Token).ConfigureAwait(true);
    }
    catch (TaskCanceledException)
    {
        Debug.WriteLine("Timed out");
    }

    x.OnReceived -= X_OnReceived;
    x.Cleanup();
}

private void X_OnReceived(object sender, SelectedItemEventArgs e)
{
    Debug.WriteLine(e.ReceivedItem);
    if (_waitCTS != null)
        _waitCTS.Cancel();
}

CodePudding user response:

You can make the handler a local that captures the CancellationTokenSource variable. EG something like:

private async Task WaitWithTimeout(string param)
{
    var CancellationTokenSource _waitCTS = new CancellationTokenSource();

    XClass x = new XClass(param);
    SelectedItemEventHandler X_OnReceived = (s, e) =>
    {
        Debug.WriteLine(e.ReceivedItem);
        _waitCTS.Cancel();
    }

    x.OnReceived  = X_OnReceived;


    try
    {
        await Task.Delay(5000, _waitCTS.Token).ConfigureAwait(true);
    }
    catch (TaskCanceledException)
    {
        Debug.WriteLine("Timed out");
    }

    x.OnReceived -= X_OnReceived;
    x.Cleanup();
}

CodePudding user response:

You can do it without CancellationTokenSource at all, for example with TaskCompletionSource:

async Task WaitWithTimeout()
{
    var tcs = new TaskCompletionSource<string>();
    XClass x = new XClass();
    // declare handler as local variable here
    Action<string> handler = (s) =>
    {
        // complete the task
        tcs.TrySetResult(s);
    };

    x.OnReceived  = handler;
    // now check which task completes first - the one from handler, or delay 5s
    var completedFirst = await Task.WhenAny(tcs.Task, Task.Delay(5000));
    x.OnReceived -= handler;
    if (completedFirst == tcs.Task)
    {
        // got the result here
        Console.WriteLine(tcs.Task.Result);
    }
    else
    {
        Console.WriteLine("Timed out");
    }
}

class XClass
{
    public event Action<string> OnReceived;
}
  • Related