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" wrote:
> On 13 mar, 09:20, Chris Fink <ChrisF...@discussions.microsoft.com>
> 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.
[quoted text, click to view] On 14 mar, 10:56, ocra...@gmail.com wrote:
> On 14 mar, 10:26, Chris Fink <ChrisF...@discussions.microsoft.com>
> wrote:
>
>
>
>
>
> > 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 loade=
d, a
> > button and the webbrowser control).
>
> > 2. In the Form1_Shown I click the button
> > =A0 =A0 =A0 =A0 private void Form1_Shown(object sender, EventArgs e)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 button1.PerformClick();
> > =A0 =A0 =A0 =A0 }
>
> > 3. The button click event loads a url into the browser control and wires=
the
> > print event for document completed.
> > =A0 =A0 =A0 =A0 private void button1_Click(object sender, EventArgs e)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 try
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string[] args =3D Environment.GetCommand=
LineArgs();
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 string URL =3D args[1].Trim();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if (!URL.StartsWith("http://") && !URL.S=
tartsWith("https://"))
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 URL =3D "http://" + URL;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 webBrowser1.Navigate(new Uri(URL));
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 webBrowser1.DocumentCompleted +=3D new
> > WebBrowserDocumentCompletedEventHandler(PrintDocument);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 catch (Exception ex)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 }
>
> > 4. the print event fires, but IMPORTANT: the actual document does not st=
art
> > printing until all these events are fired???
> > =A0 =A0 =A0 =A0 private void PrintDocument(object sender,
> > WebBrowserDocumentCompletedEventArgs e)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 try
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Print the document now that it is ful=
ly loaded.
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ((System.Windows.Forms.WebBrowser)sender=
).Print();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //WebBrowserReadyState state =3D webBrow=
ser1.ReadyState;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //webBrowser1.ShowPrintDialog();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Dispose the WebBrowser now that the t=
ask is complete.
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ((System.Windows.Forms.WebBrowser)sender=
).Dispose();
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //System.Windows.Forms.Application.Exit(=
);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 catch (Exception ex)
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 }
>
> > 5. then the default dispose fires
> > =A0 =A0 =A0 =A0 protected override void Dispose(bool disposing)
> > =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 if (disposing && (components !=3D null))
> > =A0 =A0 =A0 =A0 =A0 =A0 {
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 components.Dispose();
> > =A0 =A0 =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 =A0 =A0 base.Dispose(disposing);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 //System.Windows.Forms.Application.Exit();
> > =A0 =A0 =A0 =A0 }
>
> > 6. The print job now starts? All my code is done executing when this occ=
urs.
>
> > As you can see, I've tried calling Application.Exit in both 4 and 5 and =
they
> > are both too early. =A0I guess what I am looking for is another event, o=
ne 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
>
> > "ocra...@gmail.com" wrote:
> > > On 13 mar, 09:20, Chris Fink <ChrisF...@discussions.microsoft.com>
> > > wrote:
> > > > I am using the webbrowser control in a winforms app. =A0It is an una=
ttended
> > > > app, so as soon as the event "form1_shown" is fired the webbrowser c=
ontrol
> > > > loads a url and then wires the print event =A0as such, prints the do=
cument, and
> > > > is then supposed to exit the app.
>
> > > > webBrowser1.DocumentCompleted +=3D new
> > > > WebBrowserDocumentCompletedEventHandler(PrintDocument);
>
> > > > the print document event prints the document (without a print dialog=
) after
> > > > the webbrowser control is loaded. =A0I've noticed that the actual pr=
int does
> > > > not take place until this event code is run and control is returned =
form1. =A0
>
> > > > My question is, I want to call Application.Exit after printing is co=
mplete,
> > > > but I do not see an event available in the webbrowser control. =A0Ho=
w do I exit
> > > > out of the app after the printing is done? =A0Note, If I place the
> > > > application.exit() within the print event at the end, the applicatio=
n always
> > > > exits before the document is printed.
>
> > > > =A0 =A0 =A0 =A0 private void PrintDocument(object sender,
> > > > WebBrowserDocumentCompletedEventArgs e)
> > > > =A0 =A0 =A0 =A0 {
> > > > =A0 =A0 =A0 =A0 =A0 =A0 try
> > > > =A0 =A0 =A0 =A0 =A0 =A0 {
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Print the document now that it is=
fully loaded.
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ((System.Windows.Forms.WebBrowser)se=
nder).Print();
>
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Dispose the WebBrowser now that t=
he task is complete.
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ((System.Windows.Forms.WebBrowser)se=
nder).Dispose();
>
> > > > =A0 =A0 =A0 =A0 =A0 =A0 }
> > > > =A0 =A0 =A0 =A0 =A0 =A0 catch (Exception ex)
> > > > =A0 =A0 =A0 =A0 =A0 =A0 {
> > > > =A0 =A0 =A0 =A0 =A0 =A0 }
> > > > =A0 =A0 =A0 =A0 }
>
> > > 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();
}