开发者

How to end a thread or join it to the main thread

开发者 https://www.devze.com 2023-02-05 07:19 出处:网络
Simplified, I start a number of background threads (this is the original method), when they complete their work, they raise an event, in that event they call the original method.

Simplified, I start a number of background threads (this is the original method), when they complete their work, they raise an event, in that event they call the original method.

When the event is raised, it runs under the thread that raised it. Before I call the original method from within that event, I'd like to join back to the ma开发者_JS百科in thread (or join back in within the original method). Thus avoiding threads that can create more threads.

I guess I'm worried about the original threads never being garbage collected as they have called more threads? Will this be the case? and how can I make sure the thread has finished?

Note that I ensure only x number of threads can run at anyone time.


The threads will be independent. To push work between them you need either a basic synchronised queue, or something like the Control.Invoke method.

To wait until anothe thread has exited, you can use:

otherThreas.Join();

But in many ways the event approach is cleaner. But the ever will be on the other thread, so you may well need to switch back (for example) to a UI thread as mentioned above.

0

精彩评论

暂无评论...
验证码 换一张
取 消