Should I use BackgroundWorker or Threads for this?

That's it, I have successfully used classes Threadand BackgroundWorkerto simplify the interface for several small applications. Recently I was given the job of converting a huge piece of code from sequential to multi-threaded, and I have some questions due to some comments that I saw on this very site. The code I need to convert makes a different amount (usually a large number) of SQL Server calls, and these SQL queries can sometimes run within 30 minutes or so. Thus, multithreading is required.

I have already installed the test program with help BackgroundWorkerand they work well. However, some say that due BackgroundWorkerto the use of the thread pool they should not be used for lengthy tasks. I haven’t read anywhere (i.e. Joesph Albahari C # 4.0 in a nutshell), and this is contrary to MSDN . Should I use BackgroundWorkeror Threadfor such purposes?

Thanks in advance.

+5
source share
4 answers

, threadpool, , , , . , , , . threadpool, .

+3
+5

BackgroundWorker . ThreadPool, (BackgroundWorker ).

, 30- SQL-, , . , , , .

: - BackgroundWorker . .

+2

ThreadPool (GetMaxThreads/SetMaxThreads). , ThreadPool . , 10. 10 BackgroundWorker, 30 , , ThreadPool, 30 .

Timer, , . , , . , , MaxThreads, , 10. - 1023 (.NET 4, 2x2.26GHz ). , , , BackgroundWorker. , ThreadPool.

Personally, I use dedicated threads to be safe. Moreover, the threads, of course, will be idle, waiting for the completion of your requests. The only advantage that I see in use BackgroundWorkerin your case is that it’s easier to update the progress bar in the user interface. But if you do not show the progress of your request, this is another reason for using dedicated threads.

+1
source

All Articles