All pastes #1879501 Raw Edit

BlockingThreader

public text v1 · immutable
#1879501 ·published 2010-06-09 02:56 UTC
rendered paste body
static class BlockingThreader {
    public static void Run(params Action[] f) {
        ManualResetEvent[] els=new ManualResetEvent[f.Length];
        for (int i=0; i<f.Length; ++i)
            new Helper(RunHelper).BeginInvoke(f[i],els[i]=new ManualResetEvent(false),
                null,null);
        WaitHandle.WaitAll(els);
    }
    private static void RunHelper(Action f,ManualResetEvent sig) {
        f();
        sig.Set();
    }

    private delegate void Helper(Action f,ManualResetEvent sig);
}