Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using BackgroundWorker or Threads for this?

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.

like image 477
MoonKnight Avatar asked Oct 11 '11 12:10

MoonKnight


People also ask

What is the difference between BackgroundWorker and thread?

A BackgroundWorker is a ready to use class in WinForms allowing you to execute tasks on background threads which avoids freezing the UI and in addition to this allows you to easily marshal the execution of the success callback on the main thread which gives you the possibility to update the user interface with the ...

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

What is BackgroundWorker?

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running.

Does BackgroundWorker use ThreadPool?

The BackgroundWorker class is designed to start long-running tasks in a separate thread. This class is essentially a wrapper for the ThreadPool class and uses a thread pool in its implementation.


1 Answers

You can make use of asynchronous queries in ADO.NET.

like image 93
H-Man2 Avatar answered Oct 06 '22 00:10

H-Man2