I am working on a .NET Windows Forms application using Visual Studio 2010.
In this application, I need four background threads for underlying data transmission. When ALL the four threads finished, another round of four underlying data transmissions will be started using the four threads again.
The form UI needs to be responsive all the time. My problem is: How can I control the running of the four threads? Like: How do I know that all threads are finished? Using a volatile global counter?
To answer your question about using specifically four threads, below is a quick sketch using BackgroundWorker. The idea here is to set up four tasks, track the number of running tasks and, when they’re all done restart. For a discussion of volatile vs interlocked, see Stack Overflow question Volatile vs. Interlocked vs. lock.
This will give you what you’re asking for (four threads, responsive UI), but there’s no error handling, and there could be other problems. Is it possible that one BackgroundWorker would hang (maybe your ‘data transmission’ goes haywire) in which case you’ll end up in a bad state?