Scheduling work to occur on the thread-pool : ThreadPool : Thread C# Examples


C# Examples » Thread » ThreadPool »

 

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();
        }

}
    
   
  
   



Output

Work occurring on the thread-pool: 3
Continuing work on the main thread: 1


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Thread
» ThreadPool