C # .Net 4.0 Console app - how to stay alive until all threads complete?

Possible duplicate:
C #: waiting for all threads to complete

I have a console application that spawns some threads and then exits. Each thread takes approximately 20 seconds. It looks like the console application spawns threads and then exits before the threads can complete.

How can I tell the console application not to exit until all the threads that it spawned have completed?

+6
source share
7 answers

Are threads created for the loop? If so, then Parallel.ForEach will work:

ParallelOptions options = new ParallelOptions(); Parallel.ForEach(items, options, item=> { // Do Work here } }); 
+6
source

You can use CountDownEvent .

 using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApplication1 { class Program { static CountdownEvent countdown; static void Main(string[] args) { countdown = new CountdownEvent(1); for (int i = 1; i < 5; i++) { countdown.AddCount(); //add a count for each (BEFORE starting thread .. Thanks, Brian!) //do stuff to start background thread } countdown.Signal(); //subtract your initial count countdown.Wait(); //wait until countdown reaches zero //done! } static void backgroundwork() { //work countdown.Signal(); //signal this thread completion (subtract one from count) } } } 
+5
source

While Thread not a background thread ( .IsBackground ), the application should stay alive:

 static void Main() { Thread thread = new Thread(MoreStuff); thread.IsBackground = false; thread.Start(); Console.WriteLine("Main thread exiting"); } static void MoreStuff() { Console.WriteLine("Second thread starting"); Thread.Sleep(5000); // simulate work Console.WriteLine("Second thread exiting"); } 

Nothing more is needed. Note that ThreadPool will use background threads; The problem is that you are using ThreadPool here?

Note: if the second thread is not actually running, there may be a small race where it may exit prematurely; you can block threads starting .

+2
source

You can use Thread.Join to wait for the Thread.Join to complete.

+1
source

How do you start threads? It really depends, but if you just use the Thread class, then call yourThread[i].Join() from the main thread to make sure all threads are complete.

See Factory Tasks and Tasks to process things a lot cleaner than in previous years.

+1
source

Call Thread.Join() for all threads that you start after they start. This will block the current thread until the thread terminates.

+1
source

You should probably use synchronization and wait for all generated threads to complete their work, either by blocking the main thread with Thread.Join calls, or by using an alarm (for example, using a monitor or one of the other options for this).

Alternatively, you can simply make spawned threads run as foreground threads by setting the IsForeground (afaicr) property. This will save the application until the streams stop, but you will still see that the console window disappears as the main stream exits.

0
source

Source: https://habr.com/ru/post/887397/


All Articles