C# multi-threaded console application - Console quits before threads complete

后端 未结 6 1133
感动是毒
感动是毒 2021-02-08 23:14

I have a c# console application that creates up to 5 threads.

The threads are executing fine, but the UI thread shuts down as it finishes its work.

Is there a

6条回答
  •  醉话见心
    2021-02-08 23:59

    The threads in the ThreadPool are background threads and that means an exiting application won't wait for them to complete.

    You have a few options:

    • wait with for example a semaphore
    • wait on a counter with Sleep() , very crude but OK for a simple console app.
    • use the TPL, Parallel.ForEach(urls, url => MyMethod(url));

提交回复
热议问题