Results 1 to 2 of 2

Thread: BackGroundWorker Example

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    BackGroundWorker Example

    I see that you can use backgroundworker to show progress of a long process using C#. You would do such like so
    Code:
    private void button1_Click(object sender, EventArgs e)
    (
        backgroundWorker1.RunWorkerAsync(200);
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
         Procedure1();
         Procedure2();
         Procedure3();
         Procedure4();
         Procedure5();
         Procedure6();
         Procedure7();
         Procedure8();
         Procedure9();
         Procedure10();
    }
    Alright that's awesome! Closer to my end result than I was 2 days ago, haha. But how can I have the backgroundworker give an update each time a new procedure is entered? Something to the effect of this is what I would like to show
    Code:
    Procedure1 has begun.
    Procedure2 has begun.
    Procedure3 has begun
    etc. etc.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: BackGroundWorker Example

    Upon further further googling I discovered that this will give you a percentage update after the ProgressChanged procedure has been called. But still unclear as to how to give more detailed messages like I am wanting to do in my initial post
    Code:
    public partial class One : UserControl
    {
      private BackgroundWorker bw = new BackgroundWorker();
      
      public One()
      {
        InitializeComponent();
    	bw.WorkerReportProgress = true;
    	bw.WorkerSupportsCancellation = false;
    	bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    	bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    	bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
      }
      
      private void buttonone_Click(object sender, RoutedEventArgs e)
      {
    	bw.RunWorkerAsync();
      }
      
      private void bw_DoWork(object sender, DoWorkEventArgs e)
      {
        BackgroundWorker worker = sender as BackgroundWorker;
    	
    	Procedure1();	
    	worker.ReportProgress((i * 10));
            Procedure2();
            worker.ReportProgress((i * 10));
            Procedure3();
            worker.ReportProgress((i * 10));
            //and continue to call each procedure and update Report Progress after each completion?
      }
      
      private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
    	  this.tbProgress.Text = "Done!";
      }
      
      private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
           this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
      }
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width