Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

Application.Exit After Event Fires

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Fink - 13 Mar 2008 17:20 GMT
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 - 14 Mar 2008 17:40 GMT
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
[quoted text clipped - 30 lines]
>             }
>         }

You could try placing the Application.Exit() after the webbrowser
dispose.
Chris Fink - 14 Mar 2008 18:26 GMT
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

> On 13 mar, 09:20, Chris Fink <ChrisF...@discussions.microsoft.com>
> wrote:
[quoted text clipped - 35 lines]
> You could try placing the Application.Exit() after the webbrowser
> dispose.
ocram83@gmail.com - 14 Mar 2008 18:56 GMT
On 14 mar, 10:26, Chris Fink <ChrisF...@discussions.microsoft.com>
wrote:
> Where should I place the Application.Exit?
>
[quoted text clipped - 119 lines]
>
> - 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.
ocram83@gmail.com - 14 Mar 2008 19:29 GMT
On 14 mar, 10:56, ocra...@gmail.com wrote:
> On 14 mar, 10:26, Chris Fink <ChrisF...@discussions.microsoft.com>
> wrote:
[quoted text clipped - 130 lines]
>
> - 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.
Chris Fink - 17 Mar 2008 14:47 GMT
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

> On 14 mar, 10:56, ocra...@gmail.com wrote:
> > On 14 mar, 10:26, Chris Fink <ChrisF...@discussions.microsoft.com>
[quoted text clipped - 156 lines]
>
> This does what you want. Hope it can help.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.