Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackgroundWorker component in services

I know the BackgroundWorker should not be used in Windows Services but would anyone have a good online reference explaining why?

like image 395
Otávio Décio Avatar asked Jan 21 '11 20:01

Otávio Décio


People also ask

What is a 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.

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 ...

What is use of BackgroundWorker in C#?

BackgroundWorker is the class in System. ComponentModel which is used when you need to do some task on the back-end or in different thread while keeping the UI available to users (not freezing the user) and at the same time, reporting the progress of the same.

What is the use of BackgroundWorker in VB net?

BackgroundWorker class executes the time consuming operation on a separate background thread thereby keeping the GUI responsive to the end user. It can also report the progress of the operation periodically, cancel the operation while it is running and indicate when the operation is completed.


1 Answers

BackgroundWorker relies on a current SynchronizationContext being set in order to function. It's really intended and designed specifically for working with UI code.

It's typically better in a service to self-manage your threads, since there are no UI synchronization issues. Using the threading API (or .NET 4 Task API) is a much better option here.

like image 138
Reed Copsey Avatar answered Sep 20 '22 03:09

Reed Copsey