Skip to content

Instantly share code, notes, and snippets.

@jittuu
Created June 26, 2011 10:41
Show Gist options
  • Save jittuu/1047498 to your computer and use it in GitHub Desktop.
Save jittuu/1047498 to your computer and use it in GitHub Desktop.
class Program
{
static int count = 100000;
static int runningCount = 0;
static void Main(string[] args)
{
var dict = new Dictionary<Guid, string>();
var mre = new ManualResetEvent(false);
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem((obj) =>
{
mre.WaitOne();
while (true)
{
try
{
var key = Guid.NewGuid();
dict[key] = "A"; // this line throws IndexOutOfRangeException
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (Interlocked.Increment(ref runningCount) >= count)
{
Console.WriteLine("ThreadId: {0} done!", Thread.CurrentThread.ManagedThreadId);
break;
}
}
});
}
Console.WriteLine("Press Enter to start");
Console.Read();
mre.Set();
Console.WriteLine("starting");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment