Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Passing events to forms, backgroundworker

Back to Forum: Tech Off
  • User profile image
    JamesR

    Hi,

     

    I  am running a backgroundworker to process some long task and I would like to open a new form that has a progress bar and report the progress there. I believe I need to use some sort of event/delegate that can be passed to the new form but I am not sure how to do this. I have simple demo below of a single form that has the bw on, but I'm not sure where to go from here, can anyone help. Thanks

     

    For the second form I'm guessing I would need this, but is the delegate correct and how would I assign it the progres changed event before I load it?

     

     

    namespace BackgroundWorkerExample
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }

            public delegate void UpdateProgressBar();


            private void UpdateBar()
            {

                //Do some code to update the progress bar here


            }

     

        }
    }

     

     

     

     

     

     

    namespace BackgroundWorkerExample
    {
        public partial class Form1 : Form
        {

            private BackgroundWorker bw;

            public Form1()
            {
                InitializeComponent();
                this.bw = new BackgroundWorker();
                this.bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                this.bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
                this.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                this.bw.WorkerReportsProgress = true;

                this.button1.Click += new EventHandler(button1_Click);

            }
            private void button1_Click(object sender, EventArgs e)
            {
                if (!this.bw.IsBusy)
                {
                    this.bw.RunWorkerAsync();
                    
                }
            }

            private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                this.label1.Text = "The answer is: " + e.Result.ToString();
                this.button1.Enabled = true;
            }

            private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                this.label1.Text = e.ProgressPercentage.ToString() + "% complete";
            }

            private void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker worker = (BackgroundWorker)sender;
                for (int i = 0; i < 100; ++i)
                {
                    // report your progres
                    worker.ReportProgress(i);

                    // pretend like this a really complex calculation going on eating up CPU time
                    System.Threading.Thread.Sleep(100);
                }
                e.Result = "42";
            }

        }
    }

  • User profile image
    Maddus Mattus

    Use the RX Framework

     

    It has this functionality by default,

     

    check out: http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx

  • User profile image
    spivonious

    Or just make the form with the progress bar the owner of the backgroundworker. Or make the UpdateBar method public and have Form1 own Form2 and call UpdateBar from the ProgressChanged handler.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.