private static void RunParallel(Action action, int threads)
{
ManualResetEvent[] handles = new ManualResetEvent[threads];
for ( int i = 0; i < threads; i++)
{
handles[i] = new ManualResetEvent( false );
int i1 = i;
Thread t1 = new Thread(() =>
{
try
{
action();
}
finally
{
handles[i1].Set();
}});
t1.Start();
}
WaitHandle.WaitAll( handles );
}