private void cmdSales_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            timer2.Start();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {

            TimeSpan ts = DateTime.Now.Subtract(startDate);

            string sTime ="  ..." + ts.Minutes.ToString("00") +

               ":" + ts.Seconds.ToString("00") +

               ":" + ts.Milliseconds.ToString("000");

            toolStripStatusLabelTime.Text = sTime;

            if (toolStripProgressBar1.Value ==

                toolStripProgressBar1.Maximum)

            {

                toolStripProgressBar1.Value = 0;

            }

            toolStripProgressBar1.PerformStep();

        
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            toolStripStatusLabel1.Text = "Loading ... " +

                 "Thanks for your patience";

            frmSales objfrmSChild = frmSales.GetChildInstance();
[B]            objfrmSChild.MdiParent = this;
[/B]            objfrmSChild.Show();
            objfrmSChild.BringToFront();

        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar1.Value = 100;


            toolStripStatusLabel1.Text = "";

            toolStripProgressBar1.Value = 0;

            timer2.Stop();

            toolStripStatusLabelTime.Text = "";
        }

Progress bar is on the MDI parent form and i would like the progress bar to run after clicking on the button and when the child is loaded the progress bar will stop.

somehow i face problem when comes to "objfrmSChild.MdiParent = this;". Error msg : "Cross-thread operation not valid: Control 'frmContainer' accessed from a thread other than the thread it was created on."

How can i prevent this from happening ?

if you pass the instance of the main form to the background worker then cast it to a form, you can then easily pass that instance to the new form. Simple stuffs, Background workers take an object as a argument in the doworkeventargs, just pass it in, then pull it back out.

no need for anything complicated here.

when you call the runworker method pass to it the instance you need

backgroundWorker1.RunWorkerAsync(this);

then in the dowork event handler extract that instance to a variable casting it to the type that it is, an instance of a form.

Form formInstance = (Form)e.Argument;

Thin just pass that instance on to your new form

objfrmSChild.MdiParent = formInstance;

easy as pie :)

happy coding

Diamonddrake,

Im newbie in C#, do u can explain again(detailed) or have an example.

PS: My english is very poor...

Im not sure how much clearer i could be, so I am just going to repost your code, with the modifications that fixes the issue.

private void cmdSales_Click(object sender, EventArgs e)
        {
           [B] backgroundWorker1.RunWorkerAsync(this);[/B]
            timer2.Start();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {

            TimeSpan ts = DateTime.Now.Subtract(startDate);

            string sTime ="  ..." + ts.Minutes.ToString("00") +

               ":" + ts.Seconds.ToString("00") +

               ":" + ts.Milliseconds.ToString("000");

            toolStripStatusLabelTime.Text = sTime;

            if (toolStripProgressBar1.Value ==

                toolStripProgressBar1.Maximum)

            {

                toolStripProgressBar1.Value = 0;

            }

            toolStripProgressBar1.PerformStep();

        
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

     //get instance of main form from eventargs
     [B]Form formInstance = (Form)e.Argument;[/B]
            
toolStripStatusLabel1.Text = "Loading ... " +

                 "Thanks for your patience";

            frmSales objfrmSChild = frmSales.GetChildInstance();
            //set mdiparent to instance passed to dowork event
           [B]objfrmSChild.MdiParent = formInstance;[/B] 
            objfrmSChild.Show();
            objfrmSChild.BringToFront();

        }
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripProgressBar1.Value = 100;


            toolStripStatusLabel1.Text = "";

            toolStripProgressBar1.Value = 0;

            timer2.Stop();

            toolStripStatusLabelTime.Text = "";
        }

Happy Coding

This article has been dead for over six months. Start a new discussion instead.