Mega Code Archive

 
Categories / C# Tutorial / Thread
 

Scheduling work to occur on the thread-pool

using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime; using System.Runtime.CompilerServices; using System.Security; using System.Text; public class MainClass {     public static void Main()     {         using (ManualResetEvent mre = new ManualResetEvent(false))         {             ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadPoolWorker), mre);                          Console.WriteLine("Continuing work on the main thread: {0}",                 Thread.CurrentThread.ManagedThreadId);             mre.WaitOne();         }              }     private static void MyThreadPoolWorker(object state)     {         ManualResetEvent mre = (ManualResetEvent)state;         Console.WriteLine("Work occurring on the thread-pool: {0}",             Thread.CurrentThread.ManagedThreadId);         // set the event to let our caller know we're done:         mre.Set();     } } Work occurring on the thread-pool: 3 Continuing work on the main thread: 1