Created
January 2, 2013 23:24
-
-
Save gogsbread/4439311 to your computer and use it in GitHub Desktop.
Basic Thread play
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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