Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to run number of processes in bunches of thread without using threadPool & used EventWaitHandler for Handling
    text
    copied!<pre><code> class Process { static void Main(string[] args) { int threads = 0; int processes = 0; Console.WriteLine("Total number of processes:"); processes = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter number of parallel threads:"); threads = Convert.ToInt32(Console.ReadLine()); ManualResetEvent[] events = new ManualResetEvent[threads]; int k = 0, innercount = 0; //----running in bunches do { for (int l = 0; l &lt; threads; l++) { if (k &lt; threads) { events[l] = new ManualResetEvent(false); Runner r = new Runner(events[l], innercount); new Thread(new ThreadStart(r.Run)).Start(); Console.WriteLine("running start...{0}", innercount); k++; innercount++; } } WaitHandle.WaitAny(events); k--; Console.WriteLine("Decrement counter...{0}", k); } while (innercount &lt; processes); WaitHandle.WaitAll(events); Console.WriteLine("All finished!"); Console.ReadLine(); } } class Runner { static readonly object rngLock = new object(); static Random rng = new Random(); ManualResetEvent ev; int id; internal Runner(ManualResetEvent ev, int id) { this.ev = ev; this.id = id; } internal void Run() { int sleepTime; lock (rngLock) { sleepTime = rng.Next(2000); } Thread.Sleep(sleepTime); Console.WriteLine("Thread Runner {0}", id); if (ev.Set()) { Console.WriteLine("release thread...{0}", id); } } } </code></pre> <p>I have to run multiple threads.If one thread is finishes then start another thread.Problem is that its started all process at the same time.(Seems that this condition not working fine WaitHandle.WaitAny(events);)</p> <p>1:If 20 threads are running then 21st will be started when One thread releases from 20s thread.</p> <p>2: Without using threadpool can it be done using EventWaitHandler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload