Groups | Blog | Home
all groups > dotnet windows forms > march 2008 >

dotnet windows forms : Application.Exit After Event Fires


Chris Fink
3/13/2008 9:20:03 AM
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)
{
}
ocram83@gmail.com
3/14/2008 9:40:33 AM
On 13 mar, 09:20, Chris Fink <ChrisF...@discussions.microsoft.com>
[quoted text, click to view]

You could try placing the Application.Exit() after the webbrowser
Chris Fink
3/14/2008 10:26:03 AM
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


[quoted text, click to view]
ocram83@gmail.com
3/14/2008 10:56:24 AM
On 14 mar, 10:26, Chris Fink <ChrisF...@discussions.microsoft.com>
[quoted text, click to view]

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

ocram83@gmail.com
3/14/2008 11:29:41 AM
[quoted text, click to view]

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();
}

Chris Fink
3/17/2008 6:47:05 AM
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

[quoted text, click to view]
AddThis Social Bookmark Button