Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an app that is doing all of it's work in either the form_load or the backgroundworker. What I would like to do is make the application close itself once all of the work done by the backgroundworker is completed. Here is my code:

C#
 private void Form1_Load(object sender, System.EventArgs e
        {

              timer1.enable = true;
              timer1.interval = 1000;
              timer1.Start();
            

        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Visible = !label1.Visible;
            
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {     

                        

     
            progressBar1.Value = 200;
            ServiceController serviceController = new ServiceController("Spooler");
            serviceController.Stop();
            progressBar1.Value = 400;

            serviceController.Start();
            Thread.Sleep(3000);
            progressBar1.Value = 600;
            foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
                File.Delete(path);
            progressBar1.Value = 800;
            string szString = "~JR";
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrinterSettings = new PrinterSettings();
            printDialog.PrinterSettings.PrinterName = "Label Printer";
            Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString);
            progressBar1.Value = 1000;                    

        }

         
    }
}


I just want it to run until it's complete and then kill the form automatically. Any ideas?
Posted

1 solution

How about...

C#
this.Close();
 
Share this answer
 
Comments
Dustin Prevatt 6-Dec-12 15:40pm    
if I use that the form doesn't show, or atleast it closes right after it is opened
BC @ CV 6-Dec-12 15:41pm    
Even if you put it at the end of your code in backgroundWorker1_DoWork()?
Dustin Prevatt 6-Dec-12 15:43pm    
Yes
BC @ CV 6-Dec-12 15:49pm    
Then I would assume that the work performed by the background worker has been completed. How else could it move on to this.Close() unless the progressBar1.Value is set 1000 and all previous commands are completed. Are any of the method calls in backgroundWorker1_DoWork() asyncronous? Do you wish the form to linger for a moment to make sure the user can see the progress bar is set to 1000? If so add another sleep command before this.Close().
Dustin Prevatt 7-Dec-12 14:01pm    
You were correct!My Backgroundworker just completed faster than I expected it to. Thanks for the help!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900