I don't think it should be a problem, because you're using BeginInvoke
rather than Invoke
- the background threads will just proceed past that line without waiting for the GUI to catch up. If you're using Control.Invoke
anywhere, that could cause a deadlock.
More importantly, using Join
in your GUI thread is fundamentally a bad idea - the UI will be frozen until everything's finished. It would be better to disable any controls which could start anything new, set your isStopping
flag, and then create a new thread to wait for all the threads to stop - and when all the threads have finished, then update the UI with BeginInvoke
again. (If you're using .NET 4.5 you could also use an asynchronous method for this, creating and awaiting a task to wait for all the threads.)
Finally, if isStopping
is just a bool
field, there's no guarantee that your background threads will "see" the change from the UI thread. It's possible that making the field volatile would fix this, but the precise meaning of volatile scares me. An alternative would be to use the Interlocked
class, or make it a property which obtains a lock for both reading and writing - that ensures appropriate memory barriers are in place.