Examples of multi-threaded C # by Timer open thread

 

This example () method triggers TimerCallback commissioned to open a new thread, the thread of the specific work to TimerCallback commissioned by a static method as a parameter by the Timer tick.

a using System;
 a using the System.Threading; 

/ * This is an example of open timer multithreaded   
 * tick event 1.Timer trigger system built-commissioned TimerCallback commission. And this commission will trigger a new thread. This thread needs to be done in specific job into a static method. Passed to the delegate as a parameter. 
 * 2. If a thread with a Timer trigger will automatically start not need to use .Start () to start 
 * 3. If you do not use the thread pool threadpool then the number of threads in the system will be fully implemented so it would make if too many threads the system constantly switch consumes a lot of time between threads. So when too many threads must 
 * use a thread pool to manage so that you can improve the efficiency 
 * 4. While the timer is created and started running in the main thread, the main thread is suspended but did not affect the timer tick event is triggered on time such as the main thread to sleep five seconds in this case, but it did not affect timer.tick event triggers 
 * 5. All threads are created timer background thread that is threaded to true = .isbackground; 
 * 6. user does not create a thread pool system It will not create yourself. 
 * 7.Timer.Change (Timeout.Infinite, Timeout.Infinite) is to cancel Timer repetitive work waiting for the next enabled. 
 * Parameter 1: Timer trigger every time to wait before executing callback, 0 is triggered immediately, Timeout.Infinite never trigger callback;
 * Parameter 2: representation of each trigger interval timer, 0 represents the first time that is executed only once.              
 * / 


NamespaceTimerCreateThread 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            Console.WriteLine ( " main thread begins execution ********************* thread ID " + the Thread .CurrentThread.ManagedThreadId); 
            TimerCallback delegate_tcb = new new TimerCallback (ToDoSamething); // instantiate a new thread can be created built-commissioned --TimerCallback. Written specifically to do a static method as a parameter commission. 
            Timer = the Timer new new the Timer (delegate_tcb, null , 3000 , 2000 ); //This commission passed as a parameter in, turn on the timer tick delay of three seconds, after which a tick () event every 2 seconds, and this event will trigger TimerCallback delegate. Then start to create a thread 

            int J = 0 ;
             the while (J < . 3 ) 
            { 
                Console.WriteLine ( " main thread is executing a thread ID ********************* " + Thread.CurrentThread.ManagedThreadId); 
                Thread.Sleep ( 4000 ); // main thread of execution because sleep does not affect the tick tick time I set for 2 seconds, while the main thread to sleep four seconds // prove while timer is in It conducted the main thread but the primary thread is suspended but does not affect the timer tick event on time trigger 
                J ++ ; 
            }
            timer.Change (Timeout.Infinite, Timeout.Infinite); // finishes executing the above sentence clock stopped while loop. Timer tick is no longer excited commissioned to create a new thread * 3 = 4000 time by 12 seconds. At this point it will not trigger a new thread but the thread has triggered will not end. Regardless of whether you are or are awaiting execution
             // timer.Dispose (); // not recommended destroy. Because once destroyed can no longer restart time trigger 
            Console.WriteLine ( " thread pool is full, no longer open new thread " ); 
            Console.ReadKey (); 
        } 

        // specific work content written to this static method 
        static  void ToDoSamething ( Object I) 
        { 
            Thread.CurrentThread.IsBackground = to false ; 
            Console.WriteLine ( " this thread is being executed, the thread ID" + Thread.CurrentThread.ManagedThreadId);
            The Thread.Sleep ( 10000 ); // simulate a thread of execution requires 10 seconds. Timeout.Infinite infinite 
            Console.WriteLine ( " execution is complete, the thread ID " + Thread.CurrentThread.ManagedThreadId); 

        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/arcticfish/p/12305999.html