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

Simplified, I start several background threads (this is the original method), when they finish their work, they raise an event, in this case they call the original method.

When an event is raised, it runs under the stream that raised it. Before I call the source method from this event, I would like to join the main thread (or join the source method). Thus, avoid threads that can create more threads.

I think I'm worried that the original threads are never garbage collected because they caused more threads? Would that be so? and how can I make sure the thread is complete?

Please note that I guarantee that only x number of threads can execute at any time.

+3
source share
1 answer

Streams will be independent. To make it work between them, you need either a basic synchronized queue, or something like the Control.Invoke method.

To wait for the anot to exit, you can use:

otherThreas.Join();

But in many ways, the approach to events is cleaner. But someday it will be in a different thread, so you may need to return (for example) to the user interface thread, as described above.

+2
source

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


All Articles