Application.Exit After Event Fires

  • Thread starter Thread starter Chris Fink
  • Start date Start date
C

Chris Fink

I am using the webbrowser control in a winforms app. It is an unattended
app, so as soon as the event "form1_shown" is fired the webbrowser control
loads a url and then wires the print event as such, prints the document, and
is then supposed to exit the app.

webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);

the print document event prints the document (without a print dialog) after
the webbrowser control is loaded. I've noticed that the actual print does
not take place until this event code is run and control is returned form1.

My question is, I want to call Application.Exit after printing is complete,
but I do not see an event available in the webbrowser control. How do I exit
out of the app after the printing is done? Note, If I place the
application.exit() within the print event at the end, the application always
exits before the document is printed.

private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
// Print the document now that it is fully loaded.
((System.Windows.Forms.WebBrowser)sender).Print();

// Dispose the WebBrowser now that the task is complete.
((System.Windows.Forms.WebBrowser)sender).Dispose();

}
catch (Exception ex)
{
}
}
 
I am using the webbrowser control in a winforms app.  It is an unattended
app, so as soon as the event "form1_shown" is fired the webbrowser control
loads a url and then wires the print event  as such, prints the document, and
is then supposed to exit the app.

webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);

the print document event prints the document (without a print dialog) after
the webbrowser control is loaded.  I've noticed that the actual print does
not take place until this event code is run and control is returned form1. 

My question is, I want to call Application.Exit after printing is complete,
but I do not see an event available in the webbrowser control.  How do Iexit
out of the app after the printing is done?  Note, If I place the
application.exit() within the print event at the end, the application always
exits before the document is printed.

        private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                // Print the document now that it is fullyloaded.
                ((System.Windows.Forms.WebBrowser)sender).Print();

                // Dispose the WebBrowser now that the task is complete.
                ((System.Windows.Forms.WebBrowser)sender).Dispose();

            }
            catch (Exception ex)
            {
            }
        }

You could try placing the Application.Exit() after the webbrowser
dispose.
 
Where should I place the Application.Exit?

The current order of events are as follows:
1. the form loads using InitializeComponent (only two controls are loaded, a
button and the webbrowser control).

2. In the Form1_Shown I click the button
private void Form1_Shown(object sender, EventArgs e)
{
button1.PerformClick();
}

3. The button click event loads a url into the browser control and wires the
print event for document completed.
private void button1_Click(object sender, EventArgs e)
{
try
{
string[] args = Environment.GetCommandLineArgs();
string URL = args[1].Trim();

if (!URL.StartsWith("http://") && !URL.StartsWith("https://"))
{
URL = "http://" + URL;
}
webBrowser1.Navigate(new Uri(URL));
webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);

}
catch (Exception ex)
{
}
}

4. the print event fires, but IMPORTANT: the actual document does not start
printing until all these events are fired???
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
// Print the document now that it is fully loaded.
((System.Windows.Forms.WebBrowser)sender).Print();

//WebBrowserReadyState state = webBrowser1.ReadyState;
//webBrowser1.ShowPrintDialog();

// Dispose the WebBrowser now that the task is complete.
((System.Windows.Forms.WebBrowser)sender).Dispose();

//System.Windows.Forms.Application.Exit();

}
catch (Exception ex)
{
}
}

5. then the default dispose fires
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);

//System.Windows.Forms.Application.Exit();
}

6. The print job now starts? All my code is done executing when this occurs.

As you can see, I've tried calling Application.Exit in both 4 and 5 and they
are both too early. I guess what I am looking for is another event, one in
the winforms that runs after the print job has finished.

I'm sure its an easy solution, I just dont work with winforms often.

Thanks Again
 
Where should I place the Application.Exit?

The current order of events are as follows:
1. the form loads using InitializeComponent (only two controls are loaded,a
button and the webbrowser control).

2. In the Form1_Shown I click the button
        private void Form1_Shown(object sender, EventArgs e)
        {
            button1.PerformClick();
        }

3. The button click event loads a url into the browser control and wires the
print event for document completed.
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string[] args = Environment.GetCommandLineArgs();
                string URL = args[1].Trim();

                if (!URL.StartsWith("http://") && !URL.StartsWith("https://"))
                {
                    URL = "http://" + URL;
                }
                webBrowser1.Navigate(new Uri(URL));
                webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);

            }
            catch (Exception ex)
            {
            }
        }

4. the print event fires, but IMPORTANT: the actual document does not start
printing until all these events are fired???
        private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                // Print the document now that it is fullyloaded.
                ((System.Windows.Forms.WebBrowser)sender).Print();

                //WebBrowserReadyState state = webBrowser1.ReadyState;
                //webBrowser1.ShowPrintDialog();

                // Dispose the WebBrowser now that the task is complete.
                ((System.Windows.Forms.WebBrowser)sender).Dispose();

                //System.Windows.Forms.Application.Exit();

            }
            catch (Exception ex)
            {
            }
        }

5. then the default dispose fires
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);

            //System.Windows.Forms.Application.Exit();
        }

6. The print job now starts? All my code is done executing when this occurs.

As you can see, I've tried calling Application.Exit in both 4 and 5 and they
are both too early.  I guess what I am looking for is another event, onein
the winforms that runs after the print job has finished.

I'm sure its an easy solution, I just dont work with winforms often.

Thanks Again



You could try placing the Application.Exit() after the webbrowser
dispose.- Ocultar texto de la cita -

- Mostrar texto de la cita -

There's always an easy way. Use a MessageBox to ask the user if the
document is done printing. when the user presses Yes (Ok or whatever)
execute the Application.Exit().

If you want we can keep looking.
 
Where should I place the Application.Exit?
The current order of events are as follows:
1. the form loads using InitializeComponent (only two controls are loaded, a
button and the webbrowser control).
2. In the Form1_Shown I click the button
        private void Form1_Shown(object sender, EventArgs e)
        {
            button1.PerformClick();
        }
3. The button click event loads a url into the browser control and wiresthe
print event for document completed.
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string[] args = Environment.GetCommandLineArgs();
                string URL = args[1].Trim();
                if (!URL.StartsWith("http://") && !URL.StartsWith("https://"))
                {
                    URL = "http://" + URL;
                }
                webBrowser1.Navigate(new Uri(URL));
                webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);
            }
            catch (Exception ex)
            {
            }
        }
4. the print event fires, but IMPORTANT: the actual document does not start
printing until all these events are fired???
        private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                // Print the document now that it is fully loaded.
                ((System.Windows.Forms.WebBrowser)sender).Print();
                //WebBrowserReadyState state = webBrowser1.ReadyState;
                //webBrowser1.ShowPrintDialog();
                // Dispose the WebBrowser now that the task is complete.
                ((System.Windows.Forms.WebBrowser)sender).Dispose();
                //System.Windows.Forms.Application.Exit();
            }
            catch (Exception ex)
            {
            }
        }
5. then the default dispose fires
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
            //System.Windows.Forms.Application.Exit();
        }
6. The print job now starts? All my code is done executing when this occurs.
As you can see, I've tried calling Application.Exit in both 4 and 5 and they
are both too early.  I guess what I am looking for is another event, one in
the winforms that runs after the print job has finished.
I'm sure its an easy solution, I just dont work with winforms often.
Thanks Again
- Mostrar texto de la cita -

There's always an easy way. Use a MessageBox to ask the user if the
document is done printing. when the user presses Yes (Ok or whatever)
execute the Application.Exit().

If you want we can keep looking.- Ocultar texto de la cita -

- Mostrar texto de la cita -

This is another way:
I've added a just WebBrowser and a Timer to the form. I set the
Interval property of the timer to 2000 (2 secs). My code looks like
this:

private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
}

private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Print();
this.timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Stop();
Application.Exit();
}

This does what you want. Hope it can help.
 
This works. Thank You for your help. I still wish there was a more eloquent
solution with a Print Finished event, etc. I'll have to set the timer
interval pretty high in order to guarantee the app doesn't exit before the
printing; the higher the timer value the more sluggish the application.

Thanks again

Where should I place the Application.Exit?
The current order of events are as follows:
1. the form loads using InitializeComponent (only two controls are loaded, a
button and the webbrowser control).
2. In the Form1_Shown I click the button
private void Form1_Shown(object sender, EventArgs e)
{
button1.PerformClick();
}
3. The button click event loads a url into the browser control and wires the
print event for document completed.
private void button1_Click(object sender, EventArgs e)
{
try
{
string[] args = Environment.GetCommandLineArgs();
string URL = args[1].Trim();
if (!URL.StartsWith("http://") && !URL.StartsWith("https://"))
{
URL = "http://" + URL;
}
webBrowser1.Navigate(new Uri(URL));
webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);
}
catch (Exception ex)
{
}
}
4. the print event fires, but IMPORTANT: the actual document does not start
printing until all these events are fired???
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
// Print the document now that it is fully loaded.
((System.Windows.Forms.WebBrowser)sender).Print();
//WebBrowserReadyState state = webBrowser1.ReadyState;
//webBrowser1.ShowPrintDialog();
// Dispose the WebBrowser now that the task is complete.
((System.Windows.Forms.WebBrowser)sender).Dispose();

}
catch (Exception ex)
{
}
}
5. then the default dispose fires
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);

6. The print job now starts? All my code is done executing when this occurs.
As you can see, I've tried calling Application.Exit in both 4 and 5 and they
are both too early. I guess what I am looking for is another event, one in
the winforms that runs after the print job has finished.
I'm sure its an easy solution, I just dont work with winforms often.
Thanks Again
:
On 13 mar, 09:20, Chris Fink <[email protected]>
wrote:
I am using the webbrowser control in a winforms app. It is an unattended
app, so as soon as the event "form1_shown" is fired the webbrowser control
loads a url and then wires the print event as such, prints the document, and
is then supposed to exit the app.
webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(PrintDocument);
the print document event prints the document (without a print dialog) after
the webbrowser control is loaded. I've noticed that the actual print does
not take place until this event code is run and control is returned form1.
My question is, I want to call Application.Exit after printing is complete,
but I do not see an event available in the webbrowser control. How do I exit
out of the app after the printing is done? Note, If I place the
application.exit() within the print event at the end, the application always
exits before the document is printed.
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
// Print the document now that it is fully loaded.
((System.Windows.Forms.WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((System.Windows.Forms.WebBrowser)sender).Dispose();
}
catch (Exception ex)
{
}
}
You could try placing the Application.Exit() after the webbrowser
dispose.- Ocultar texto de la cita -
- Mostrar texto de la cita -

There's always an easy way. Use a MessageBox to ask the user if the
document is done printing. when the user presses Yes (Ok or whatever)
execute the Application.Exit().

If you want we can keep looking.- Ocultar texto de la cita -

- Mostrar texto de la cita -

This is another way:
I've added a just WebBrowser and a Timer to the form. I set the
Interval property of the timer to 2000 (2 secs). My code looks like
this:

private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
}

private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Print();
this.timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Stop();
Application.Exit();
}

This does what you want. Hope it can help.
 
Back
Top