How can I wait for a thread to finish with .NET?

前端 未结 10 2298
有刺的猬
有刺的猬 2020-11-22 10:27

I\'ve never really used threading before in C# where I need to have two threads, as well as the main UI thread. Basically, I have the following.

public void S         


        
10条回答
  •  醉话见心
    2020-11-22 11:07

    I took a little different approach. There is a counter option in previous answers, and I just applied it a bit differently. I was spinning off numerous threads and incremented a counter and decremented a counter as a thread started and stopped. Then in the main method I wanted to pause and wait for threads to complete I did.

    while (threadCounter > 0)
    {
        Thread.Sleep(500); // Make it pause for half second so that we don’t spin the CPU out of control.
    }
    

    This is documented in my blog post: http://www.adamthings.com/post/2012/07/11/ensure-threads-have-finished-before-method-continues-in-c/

提交回复
热议问题