Click here to Skip to main content
13,148,107 members (73,551 online)
Rate this:
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;

        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");
            progressBar1.Value = 400;
            progressBar1.Value = 600;
            foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
            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 9:31am

1 solution

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

Solution 1

How about...

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
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2017
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