c# - Is there a better way to wait for queued threads? -


Is there a better way to wait for a thread than before executing another process?

Currently I am doing:

  this.workerLocker = new object (); // global variable this.RunningWorkers = arrayStrings.Length; // Global Variable // Start Process Process (string strings in string string) {ThreadPool.QueueUserWorkItem (this.DoSomething, someString); Thread.Sleep (100); } // All queued threads lock (this. Clockworker) // Execution waiting for Global Variable {while (this.RunningWorkers & gt; 0) {Monitor.Wait (this.workerLocker); }} // Give some more console. Farewell line ("END");  

  // method DoSomething () Definition Public Zero DoSomething (Object Data) {// Make a slow process .... . . Lock (this. Worker locker) {this.RunningWorkers--; Monitor.Pulse (this.workerLocker); }}  

You probably want to take a look at AutoResetEvent and ManualResetEvent.

They are in fact for this situation (waiting for Threadpool thread, before doing something "before").

You want to do something like this:

  Fixed zero main (string [] args) {list & lt; ManualResetEvent & gt; Reset = new list & lt; Manualrass event & gt; (); Foreach (enumerable in version X. Category (1, WorkACCACC)) {Manual Reset Avenue ResetAvent = New Manual Reset Event (); Threadpool.QueuswarWarkTime (dojooming, reset event); ResetEvents.Add (resetEvent); } // Wait for all manual reset events WaitHandle.WaitAll (resetEvents.ToArray ()); // You probably want to use an array instead of a list, for example a list was very easy :-)} Public Static Zero DoSomething (Object Data) {ManualResetEvent resetEvent = Data as ManualResetEvent; // some resetEvent.Set (); }  

Edit: forgot to mention that you can wait a thread, any thread and so forth as well. Also on the basis of your situation, AutoResetAvent can make things a bit easier because it (as the name implies) can automatically sign events: -)


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -