Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 2, 2013 23:24
Show Gist options
  • Save gogsbread/4439311 to your computer and use it in GitHub Desktop.
Save gogsbread/4439311 to your computer and use it in GitHub Desktop.
Basic Thread play
using System;
using System.Threading;
namespace dotNetPlayGround
{
class Threads
{
static void Main()
{
Console.WriteLine("I am executing in thread {0}",Thread.CurrentThread.ManagedThreadId);
Thread fg = new Thread(new ParameterizedThreadStart(Method1));
AutoResetEvent reset = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(Method2), reset);
fg.Start();
Console.WriteLine("Waiting for other threads");
Thread.CurrentThread.Join(new TimeSpan(0, 0, 2));
reset.WaitOne(new TimeSpan(0, 0, 2));
}
static void Method1(object state)
{
Console.WriteLine("I am executing in thread {0}",Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
}
static void Method2(object state)
{
Console.WriteLine("I am executing in thread {0}",Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
((AutoResetEvent)state).Set();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment