PLINQ Adventure Land - WaitForAll

Posted by adweigert on ASP.net Weblogs See other posts from ASP.net Weblogs or by adweigert
Published on Sun, 23 May 2010 17:28:00 GMT Indexed on 2010/05/23 17:41 UTC
Read the original article Hit count: 368

Filed under:
|
|
|

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;
    }
}

© ASP.net Weblogs or respective owner

Related posts about c#

Related posts about LINQ