Skip to content

Instantly share code, notes, and snippets.

@Konard
Last active December 17, 2015 02:39
Show Gist options
  • Save Konard/5537926 to your computer and use it in GitHub Desktop.
Save Konard/5537926 to your computer and use it in GitHub Desktop.
ThreadArrayExtensions is static class (module) containing extension-methods for System.Threading.Thread[] type.
using System;
using System.Threading;
namespace Helpers
{
public static class ThreadArrayExtensions
{
public static Thread[] StartAll(this Thread[] threads, params object[] values)
{
if (values.Length == 0)
{
for (int i = 0; i < threads.Length; i++)
threads[i].Start();
}
else if (values.Length == 1)
{
for (int i = 0; i < threads.Length; i++)
threads[i].Start(values[0]);
}
else
{
for (int i = 0; i < threads.Length; i++)
threads[i].Start(values[i]);
}
return threads;
}
public static Thread[] WaitAll(this Thread[] threads)
{
for (int i = 0; i < threads.Length; i++)
threads[i].Join();
return threads;
}
public static Thread[] WaitAll(this Thread[] threads, TimeSpan timeout)
{
for (int i = 0; i < threads.Length; i++)
threads[i].Join(timeout);
return threads;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment