.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Threading using BackgroundWorker class

Posted By:Jean Paul       Posted Date: September 28, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Introducing the BackgroundWorker class instead of using Thread class.
 

BackgroundWorker

Instead of using Thread class for asynchronous operations we can use the BackgroundWorker class in .Net.

This class gives you much flexibility over Thread class.  The important events we should use are

DoWork and RunWorkerCompleted.  The DoWork event should be handled to process the core work of thread.

The RunWorkerCompleted event is called when the DoWork method is processed.

Example:

private void CodeSample()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            worker.RunWorkerAsync();
        }

        private object value;

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (value != null)
            {
                // Process
            }
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Get value from a web service 
            value = GetValueFromWebService(); 
        }
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend