The Join method only waits for the instance of the thread that you joined, so Simulator.Start just creates some threads, and it ends because the Join result is returned and your main thread ends. But still, your application is live (the reason some other Foreground threads continue to work).
generate a report never gets executed? Why?
The process will end when all Foreground Threads complete. therefore, as soon as your child threads return from the PumpMessages method, when you call RequestStop in a loop, all your foreground threads terminate
public void Stop() {
There was little misleading that I said that all foreground threads die after the loop . To make it clear, let's say that we gave instructions so that worker threads stop working, so all threads may or may not die before executing the GenerateReport method. yes there is Race If work flows win the race, we lose it and vice versa. sometimes your GenerateReport can run without any problems.
How to fix it? We are just waiting for the completion of all our work flows. what he.
public void Start() { _threads.Clear(); _workers.Clear(); for (int i = 0; i < _options.NumberOfClients; i++) { _workers.Add(new Worker()); _threads.Add(new Thread(_workers.Last().PumpMessages)); _threads.Last().Start(); } foreach (var t in _threads) t.Join(); }
source share