Example of multi-thread scheduling ·····················

Source: Internet
Author: User

Private manualresetevent _ isserverclosed = new manualresetevent (false );

Private void startthreads ()
{
// Generate four ABCD threads
For (INT I = 0; I <4; I ++ )//
{
System. Threading. thread t = new thread (New threadstart (receivedata ));
T. Start ();
}
}

// Stop receiving and run on the main thread.
Private void button6_click (Object sender, eventargs E)
{
_ Isserverclosed. Set ();
}

// Run on the new thread
Private void receivedata ()
{
// Wait 100 milliseconds to receive data,
While (! _ Isserverclosed. waitone (100, false ))
{
// Receive data ......

For (INT I = 0; I <1000000; I ++)
{

/// This. richtextbox1.text + = I. tostring () + "\ r \ n ";
// System. Diagnostics. process. Start ("cmd.exe", I. tostring () + "\ r \ n ");

}
}

}

Private void button#click (Object sender, eventargs E)
{
Richtextbox1.text = "";
Button1.enabled = false;
Mutex1 = new mutex (true); // mutex object (a scheduling command)
Mutex2 = new mutex (true );
Mutex3 = new mutex (true );
Mutex4 = new mutex (true );
Mutex5 = new mutex (true );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "multi-thread scheduling example"); // update the progress information when the main thread is paused.
Thread T1 = new thread (New threadstart (thread1); // defines four threads
Thread t2 = new thread (New threadstart (thread2 ));
Thread T3 = new thread (New threadstart (thread3 ));
Thread t4 = new thread (New threadstart (thread4 ));
T1.start (); // use the mutex. waitall () method to wait for all objects in a mutex array (mutex4 and mutex5) to be released
T2.start (); // use the mutex. waitone () method to wait for the release of mutex3
T3.start (); // use the mutex. waitany () method to wait for any object in a mutex array to be released
T4.start (); // use waithandle. waitall to wait for the end of all threads
Thread. Sleep (10 );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "Five Thread Scheduling commands mutex1 -- mutex5"); // update progress information when the main thread is paused
Thread. Sleep (2000 );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "Release mutex1 in the main thread"); // update the progress information when the main thread is paused.
Mutex1.releasemutex (); // The thread3 end condition of the thread is met.
Thread. Sleep (2000 );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "Release mutex3 in the main thread"); // update progress information when the main thread is paused
Mutex3.releasemutex (); // The thread2 end condition of the thread is met.
Thread. Sleep (2000 );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "Release mutex4 in the main thread"); // update the progress information when the main thread is paused.
Mutex4.releasemutex ();
Thread. Sleep (2000 );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "Release mutex5 in the main thread"); // update the progress information when the main thread is paused.
Mutex5.releasemutex (); // The mutex1 end condition of the thread is met.
Button1.enabled = true;
}

Private Static mutex mutex1, mutex2, mutex3, mutex4, mutex5; // create a mutex object (scheduling command)
Private Static autoresetevent event1 = new autoresetevent (false); // indicates the end of the thread. If the light is not finished, false indicates that the end is true.
Private Static autoresetevent event2 = new autoresetevent (false );
Private Static autoresetevent event3 = new autoresetevent (false );
Private autoresetevent [] events = new autoresetevent [] {event1, event2, event3}; // thread group end information light

Private delegate void changestring (string txt); // This proxy can call the set text box asynchronously (multiple parameters are allowed)

Public void thread1 ()
{
Richtextbox1.invoke (New changestring (setrichtextbox1text), new object [] {"thread thread1 startup, mutex. waitall, wait for mutex4 and mutex5 to release the signal "}); // from vs2005, attaching a value to the main program control component is considered unsafe.
Mutex [] GMS = new mutex [2];
GMS [0] = mutex4; // create a mutex array as a parameter of the mutex. waitall () method.
GMS [1] = mutex5;
Mutex. waitall (GMS); // wait until both mutex1 and mutex2 are released
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread1 end"); // from vs2005, attaching a value to the main program control component is considered unsafe.
Event1.set (); // when the thread ends, set event1 to a signal state.
}

Public void thread2 ()
{
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread thread2 startup, mutex3.waitone, waiting for mutex3 to release signal"); // from vs2005, attaching a value to the main program control component is considered unsafe
Mutex3.waitone (); // wait for mutex3 to be released
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread2 end"); // from vs2005, attaching a value to the main program control component is considered unsafe.
Event2.set (); // when the thread ends, set event2 to a signal state.
}

Public void thread3 ()
{
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread thread3 startup, mutex. waitany, waiting for a release signal in mutex1 and mutex2 "); // from vs2005, attaching a value to the main program control component is considered unsafe
Mutex [] GMS = new mutex [2];
GMS [0] = mutex1; // create a mutex array as a parameter of the mutex. waitany () method.
GMS [1] = mutex2;
Mutex. waitany (GMS); // wait for any mutex object in the array to be released
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread3 end"); // from vs2005, attaching a value to the main program control component is considered unsafe.
Event3.set (); // when the thread ends, set event3 to a signal state.
}
Public void thread4 ()
{
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread thread4 startup, waithandle. waitall, wait for all threads to end "); // from vs2005, attaching a value to the main program control component is considered unsafe.
Waithandle. waitall (events );
Richtextbox1.invoke (New changestring (setrichtextbox1text), "thread4 ends, all threads ends"); // from vs2005, it is considered unsafe to attach a value to the main program control component
}
Private void setrichtextbox1text (string txt) // This part is actually called by the main thread (multiple parameters are allowed)
{
Richtextbox1.text + = TXT + "\ r \ n ";
Richtextbox1.refresh ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.