Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
How to close a form and show another form if the progress of background worker reaches 100%..Form is showing but the form supposed to close isn't closing...
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Stopwatch sw = Stopwatch.StartNew();
 
        sw.Stop();
        var elapsedMs = sw.ElapsedMilliseconds;
        int sp = int.Parse(sw.ElapsedMilliseconds.ToString());
        // Your background task goes here
        for (int i = 0; i <= 100; i++)
        {
            // Report progress to 'UI' thread
            backgroundWorker1.ReportProgress(i);
            // Simulate long task
            System.Threading.Thread.Sleep(100);
        }
    }
 
   void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
            if (progressBar1.Value == 100)
            {
                Form1 fm1 = new Form1();
                fm1.Close();
                Form2 fm = new Form2();
                fm.Show();
            }
        }
ALso tried under
private void myBGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Done");
            Form1 fm1 = new Form1();
            fm1.Close();
            Form2 fm = new Form2();
            fm.Show();
        }
Posted 28-Apr-13 0:02am
Edited 28-Apr-13 1:52am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No, no - it doesn't work like that.
You don't want to create a new instance of the current form and try to close it, you want to "close" the current instance, and show a new form. Unfortunately, that will probably kill you application as well.
 
So, your choices are:
1) Create a "master" form which is never shown, and which displays Form1 until teh job is doen, then closes it and shows teh second form.
2) Hide the current form, and close it when the second form shuts.
The second is easier to show you:
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
   // The progress percentage is a property of e
   progressBar1.Value = e.ProgressPercentage;
   if (progressBar1.Value == 100) 
      {
      Hide();
      Form2 fm = new Form2();
      fm.ShowDialog();
      Close();
      }
   }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 309
2 BillWoodruff 174
3 /\jmot 160
4 Suraj Sahoo | Coding Passion 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 28 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100