Timers vs BackGroundWorkers for Multithreading in C#
Hello everyone, in this article we are going to talk about Timers and Bacgorundworkers in C#. We will explain them first and we will make a comparisation between them with an example.Let's begin.
Firstly, What is Backgroundworker?
Sometimes we may need a function which takes so many time to completed its operation. Normally this will happen on main thread and during this operation the program will not respond to user. For preventing this we need another thread that work at background to execute related function. Background worker component is dedicated for this. With this component the user start a function within a new thread and keep doing what user does with main thread.
For example, for some operations we may need an internet connection to check something from server. Normally if the main thread does not need the check results, it is good to let the thread to make its operations. But if we do not make it with another thread it will happen on main thread and we will have to wait to finish operation. With Backgroundworker
What is Timer?
The Timers are using to make some operations at specified intervals repedatelly. When we set the function and interval Timer component will execute the function at the configured intervals at background.
For example we can make a server connection and check a value from server at background with Timer. Generally timers being used at the digital watch applications. We update the time values at every seconds.
Both of them use Threadpool to create a thread. Backgroundworker creates the thread through asynchronously delegate invoking and the Timer creates the thread from the Threadpool or sometimes it can use the UI thread.
There are effective events and properties which belong to backgroundworkers. With these event we can monitor the progress status of the backgroundworker while working at background and we can get a signal when operation completed. Also we can bind a progressbar to the backgroundworker to see percentage of completition of the commanded operation during the background operation. If we do not have these signals and methods probably we are going to need another thread or cases to check these progresses. Timer does not have properties and methods like Backgroundworker has.
The Timer executing intervals depends on the time but sometimes the timer could not figure out the intervalsIt waits for the finish declared operation. At the Background worker we can set the waiting spans we can not set the intervals.
Both of them can update the User Interface from background without any freezing the main thread. Generally backgroundworker component is dedicated to make updates from background so it is the better solution for background operation.
So many developer using the BackgroundWorker for the methods and functions. Because developers want to show the operation progress to the users and inform them what is going on.
That is all in this article.
Have good backgroundworking.
I wish you have healthy days.
Burak Hamdi TUFAN
Comments
Thank you !
2022/10/12 09:19:01