All, I have used the Thread and BackgroundWorker classes with success to facilitate a smooth UI for a few small-scale applications. I have recently been given the job of converting a huge piece of code from serial to multi-threaded and I have some questions due to some comments I have seen on this very site. The code I have to convert makes a varying amount (usually a large number) of calls to SQL Server and these SQL queries can sometimes run for 30 minutes or so. As such, multi-threading is required.
I have already setup a test program using BackgroundWorker and these run well. However, some say that due to the BackgroundWorker using the Thread-Pool they should not be used for long running tasks. I have not read this anywhere (i.e. Joesph Albahari C# 4.0 In a Nutshell), and this contradicts MSDN. Should I be using BackgroundWorker or Thread for such purposes?
Thanks in advance.
The idea behind the threadpool is that it reuses threads and thus amortizes the cost of creating new threads. Creating a thread may be a very expensive operation, so it makes sense reuse threads if possible. If you schedule long running jobs on the threadpool, you force it to create additional threads and thus reduce the benefit of reusing threads.