Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
 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 6-Dec-12 10:31am

1 solution

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

Solution 1

How about...
 
this.Close();
  Permalink  
Comments
Dustin Prevatt at 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 at 6-Dec-12 15:41pm
   
Even if you put it at the end of your code in backgroundWorker1_DoWork()?
Dustin Prevatt at 6-Dec-12 15:43pm
   
Yes
BC @ CV at 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 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 6 Dec 2012
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