Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
New MS SQL 2008 Available - DiscountASP.NET
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Threading » Background worker simplified

Background worker simplified

This article looks at the Background Worker Technology and encapsulates it into a simple form that can be used over and over to run your background tasks.

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 364
Total page views :  11787
Rating :
 4/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
FormProgressViewer.zip
 
ArticleAd
Become a Sponsor



Introduction:

Have you ever tried to get a background worker to display a progress bar.  Every article I have seen written goes on and on about how to implement the background worker but never discusses how to put the whole meal deal together.  That is to get the form to show the actual progress.

See the following examples

http://msdn2.microsoft.com/en-us/library/8xs8549b.aspx

http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.htmlhere


is a very good c# corner article

http://www.csharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx

But this is all very complicated.  I don't necessarily want to fill a data grid or make a SQL call or calculate Fibonacci numbers. But I do want to do "stuff" from time to time and I don't want to have to rethink the whole threading process over and over.  So I created a simple form that you pass the dowork method to. Then just display the form viola you have a nice progress bar running over and over.

So here is the FormProgressViewer image







 
I added some basic elements. A lable so that the messages passed to the backgroundworker could be displayed. A richtextbox (default not visible but infront of the lable) to hold all of the messages. A toggle button to determine what you want to display (current message or all messages). A start button (not visible during task running) A cancel button (disabled during non running tasks) and an OK button to close form when task is complete.

Here is the FormProgessViewer when task complete or cancled


 
The nuts and bolts of the the form are as such

First you need to assign a method that will do the work. The trick is that this method can be created out side of the form as long has it matches the signature of the DoWorkEventHandler. Here is the property for the FormProgressViewer.

public DoWorkEventHandler SET_WORKER_METHOD
{
    set
   
{
       
bw = new BackgroundWorker();
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
       
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
       
bw.WorkerReportsProgress = true;
       
bw.WorkerSupportsCancellation = true;
       
bw.DoWork += value;
       
_AsyncMethod = value; // just so I can check to see if it exists
       
SetButtonState();
   
}
}

Once this method is set then you  call the START_WORK() method. You will notice that the START_WORK() method sets a property of the timer component "this.timer1.Enabled = true;". 

public
void START_WORK()
{
   
if (_AsyncMethod == null)
   
{
       
MessageBox.Show(this,"No background task assigned. Task Complete!","No Task!");
       
return;
   
}
   
this._UserCanceled = false;
   
this._FinalResult = null;
   
this._Message_Cummulative += this._Message = "Process Started\n";
   
bw.RunWorkerAsync();
   
_HasRunOnce = true;
   
SetButtonState();
   
this.timer1.Enabled = true;
}

This timer is critical. If you don't have some method of having the form update it self the the form just sits there and nothing gets updated.  For some reason changing the values in the bw_ProgressChanged will not update the form So you need a separate event to occur that will update the form.

private void timer1_Tick(object sender, EventArgs e)

{

    this.lblMessages.Text = _Message;

    if (this.richTextBox1.Text != _Message_Cummulative)

    this.richTextBox1.Text = _Message_Cummulative;

    if (bw != null)

    if (!bw.IsBusy)

    this.timer1.Enabled = false;

    if (_Continuos_progress)

    {

        int newValue = this.progressBar1.Value + 1;

        this.progressBar1.Value = (newValue > 100 ? 0 : newValue);

    }

    else

    this.progressBar1.Value = _ProgValue;
}

You can see the Calling Form is very simple. The button just creates the FormProgressViewer. Assings the method described on this form and starts the task then displays the FormProgressViewer. Now I have something I can user over and over.

The entire calling Form

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }

    private void butTestBW_Click(object sender, EventArgs e)

    {

        FormProgressViewer fpv = new FormProgressViewer();

        // Some Optional Parameters

        fpv.Text = "SOME TEXT"; // Sets the dialog Caption

        fpv.RUN_ONLY_ONCE = false; // lets the user cancel and click start over and over

        fpv.CONTINOUS_PROGRESS = true; // Progress reporting does not update the progressbar. The internal timer does so   
        that the bar just cycles.

        // Required Parameters

        fpv.SET_WORKER_METHOD = new DoWorkEventHandler(bw_DoWork);

        fpv.START_WORK(); // best to set dialog options before you start the working thread.

        // Have to display the form so that you can see what is going on.

        fpv.ShowDialog();

    }

    /// <summary>

    /// The simple task I want to complete!!!!!

    /// </summary>

    void bw_DoWork(object sender, DoWorkEventArgs e)

    {

        BackgroundWorker bw = (BackgroundWorker) sender;

        for (int i = 0; i < int.MaxValue/2; i++)

        {

            if (bw.WorkerSupportsCancellation)

            if (bw.CancellationPending)

            {

                i = int.MaxValue / 2 + 1;

                bw.ReportProgress(50, "User Canceled");

                // e.Cancel = true; Cannot read e.Result if e.Cancle is true

                e.Result = 60;

                //bw.CancellationPending = false;

            }

            if (i == 99)

            1.ToString();

            if (i % 100 == 0)

            {

                bw.ReportProgress((new Random(i)).Next(100) , "" + i + ": Message Sent: MaxValue = " + int.MaxValue);

            }

            System.Threading.Thread.Sleep(10); //Tried this to get form FormProgressViewer to redraw!

        }

    }

}

A couple of notes I did int.MaxValue/2 becuase I usually exit the loop by setting i = MaxValue but then what happens is the i++ occurs first and makes a -ve max value. (This has to do with the binary math). The Thread.Sleep is important. This task is so fast that it has the potential to generate messages so quickly that the FormProgressViewer cannot handle all of the messages and will hang.  So this controls this. An improvement could be to control this in FormProgressViewer.

A couple of other points about the source code that are worth looking at but not necessarily related to the major jest of this article. I used a panel to hold the 3 buttons on the form. I used docking for the progressbar, label, and richtextbox. The progressbar label and richtextbox are inside a panel so this allows the docking to hold the layout nicely. Then I used anchors on the panels themselves to manage the layout when the form resizes. Just a good example on how to use some of the layout features provided by .Net. I really don't see many articles on how to control layout of forms with ease.  Probably becuase that is usually more boring to read then how to make a progresbar spin around.

Thank you for reading this article. Your feedback is truely apprecated.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Daniel
Dr. Krueger has been programming with .Net technologies since 2004. He has extensive bacground with basic .Net functionality and MySQL data manipulation.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
FormProgressViewer.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved