Encountered in the development of such requirements, the main thread to open multiple sub-threads to process data to improve efficiency, after all the child threads to complete the task, the main thread continues to complete the subsequent operations. After some thinking, the idea came out, my approach is to define a global integer static variables, each sub-thread after the completion of the task variable plus 1. The main thread has a while dead loop, each time the loop to determine the value of the variable, if the value of the number of threads open indicates that all child threads have completed the task, and then jump out of the loop to continue to perform subsequent operations. Logically, there was no problem, so the code run test was completed quickly. The result is naturally as well as expected.
Run the process found high CPU utilization, if the execution of too long can always feel the fan turn is very strong, very worried about their own laptop burned. So think of the optimization code, but how to start from where, think of a very long thought of a very two ways, in the loop Riga sentence Thread.slee (). This way, the main thread sleeps for a period of time after each loop to reduce the high utilization rate of the cyclic CPU. This optimizes the code and runs for a while. But also did not compare with before, efficiency is high or low unknown, in the mind always muttering such code look very awkward. Although not a few lines of code, anyway, looking at is uncomfortable, not pleasing to the eye, and then have the idea of optimization. This time optimization is no longer the self-pondering, should Baidu a bit, see how others are realized.
After a search, finally found the keyword "manualresetevent", and then the MSDN review related documents, the following is the code of the exercise
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Manualreseteventtest{class Program {static void Main (string[] args) {Manualreseteventcon text[] contexts = new MANUALRESETEVENTCONTEXT[5]; manualresetevent[] manualresetevents = new MANUALRESETEVENT[5]; for (int i = 0; i < 5; i++) {ManualResetEvent e = new ManualResetEvent (false); Manualresetevents[i] = e; Contexts[i] = new Manualreseteventcontext (e, dosomething) {ID = i}; ThreadPool.QueueUserWorkItem (Contexts[i]. Invoke, Contexts[i]); } waithandle.waitall (manualresetevents); Console.WriteLine ("All sub-threads execute complete"); Console.read (); } static void DoSomething (object state) {Manualreseteventcontext context = state as Manualresete Ventcontext; Do ... Thread.slEEP (1000*60); Console.WriteLine (String. Format ("{0} sub-thread execution complete", context.id)); }} public class Manualreseteventcontext {public int ID {get; set;} Public ManualResetEvent ManualResetEvent {get; set;} Private WaitCallback _callback; Public Manualreseteventcontext (ManualResetEvent ManualResetEvent, WaitCallback callback) {this. ManualResetEvent = ManualResetEvent; This._callback = callback; public void Invoke (object state) {try {_callback (state); You must call this method Manualresetevent.set () after the finally {//Sub-threading operation is complete; } } }}
Operation Result:
About inter-thread communication