PLINQ Adventure Land - WaitForAll

PLINQ is awesome for getting a lot of work done fast, but one thing I haven't figured out yet is how to start work with PLINQ but only let it execute for a maximum amount of time and react if it is taking too long. So, as I must admit I am still learning PLINQ, I created this extension in that ignorance. It behaves similar to ForAll<> but takes a timeout and returns false if the threads don't complete in the specified amount of time. Hope this helps someone else take PLINQ further, it definitely has helped for me ...

 

public static bool WaitForAll<T>(this ParallelQuery<T> query, TimeSpan timeout, Action<T> action)
{
    Contract.Requires(query != null);
    Contract.Requires(action != null);

    var exception = (Exception)null;
    var cts = new CancellationTokenSource();
            
    var forAllWithCancellation = new Action(delegate
    {
        try
        {
            query.WithCancellation(cts.Token).ForAll(action);
        }
        catch (OperationCanceledException)
        {
            // NOOP
        }
        catch (AggregateException ex)
        {
            exception = ex;
        }
    });
            
    var mrs = new ManualResetEvent(false);
    var callback = new AsyncCallback(delegate { mrs.Set(); });
    var result = forAllWithCancellation.BeginInvoke(callback, null);
            
    if (mrs.WaitOne(timeout))
    {
        forAllWithCancellation.EndInvoke(result);

        if (exception != null)
        {
            throw exception;
        }

        return true;
    }
    else
    {
        cts.Cancel();
        return false;
    }
}

3 Comments

Comments have been disabled for this content.