I have a stylistic question about the choice of background thread implementation I should use on a windows form app. Currently I have a BackgroundWorker
on a form that has an infinite (while(true))
loop. In this loop I use WaitHandle.WaitAny
to keep the thread snoozing until something of interest happens. One of the event handles I wait on is a "StopThread
" event so that I can break out of the loop. This event is signaled when from my overridden Form.Dispose()
.
I read somewhere that BackgroundWorker
is really intended for operations that you don't want to tie up the UI with and have an finite end - like downloading a file, or processing a sequence of items. In this case the "end" is unknown and only when the window is closed. Therefore would it be more appropriate for me to use a background Thread instead of BackgroundWorker
for this purpose?
From my understanding of your question, you are using a BackgroundWorker
as a standard Thread.
The reason why BackgroundWorker
is recommended for things that you don't want to tie up the UI thread is because it exposes some nice events when doing Win Forms development.
Events like RunWorkerCompleted
to signal when the thread has completed what it needed to do, and the ProgressChanged
event to update the GUI on the threads progress.
So if you aren't making use of these, I don't see any harm in using a standard Thread for what you need to do.