Follow in Twitter & Facebook



Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools



Thursday, May 3, 2012

BackgroundWorker Control in C#.NET

Introduction:
Here in this article, we will discuss about Backgroundworker control in C#.NET. How it exactly helps us for running our applications.

Description:
If we are going to use multithreaded applications, then we can able to do many time consuming tasks asynchronously.

Backgroundworkers make threads easy to implement in windows forms. So in case of Intensive tasks need to be done on another thread so that the UI doesn't freeze. This is our main aim that the UI doesn't freeze.
When your application loads, it runs on a single thread. Generally this is the thread that all of your UI objects have been created on and this is the thread that all of your code execution is performed on.

The UI also use the that same thread to paint the UI objects. So when your are running a long task or heavy task some times the title bar changes to the name of the title with (Not Responding).

So for this case only the Backgroundworker will going to help us by implementing that multithreading concepts.
This object is designed to simply run a function on a different thread and then call an event on your UI thread when it completes. So .NET has made the Backgroundworker object avaible to simplify threading.

You can use this one to do some heavy calculations like you can use in case of database acess or file searching .

While using this control, the first thing is to add the Backgroundworker control to the application and the easiest way to use this control is to drag it from the toolbox and drop it on the form. Its available under the components tab in the toolbox.



Backgroundworker class is from the System.ComponentModel namespace. The backgroundworker is event driven so you need to follow certain steps

Invoke the Backgroundworker's DoWork class from your application.

Give DoWork some work to do by adding some code in the BackgroundWorker1.DoWork method.

After the code in the DoWork method is done, the event RunWorkerCompleted is invoked.
Through the RunWorkerCompleted method, we will retrive our returns values from the 2nd thread.




0 on: "BackgroundWorker Control in C#.NET"