Problem with quitting an application
-
Hi, I have an application used to print certain product info sheets. It is normally launched as a conventional application and works exactly as intended. The associated database editor can also launch it (using Process.Start()) to print any changed product sheets; in that case the application prints correctly but remains open after completing its work. If I call this.Close() explicitly from anyplace other than the Quit menu item handler an exception is thrown; using Application.Exit() or Environment.Exit() has no effect. Looking at the Process documentation on MSDN yields no obvious clues. How can I have the application quit automatically after being launched by the database editor? Thanks in advance, -Frank Alviani
-
Hi, I have an application used to print certain product info sheets. It is normally launched as a conventional application and works exactly as intended. The associated database editor can also launch it (using Process.Start()) to print any changed product sheets; in that case the application prints correctly but remains open after completing its work. If I call this.Close() explicitly from anyplace other than the Quit menu item handler an exception is thrown; using Application.Exit() or Environment.Exit() has no effect. Looking at the Process documentation on MSDN yields no obvious clues. How can I have the application quit automatically after being launched by the database editor? Thanks in advance, -Frank Alviani
You really have no control over that. The application that you launch has to supply that kind of functionality, maybe through a command line switch. You'll have to consult with the documentation or the manufacturer of the app you're launching.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
You really have no control over that. The application that you launch has to supply that kind of functionality, maybe through a command line switch. You'll have to consult with the documentation or the manufacturer of the app you're launching.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks for the info. I wrote the app myself, and know *when* I should be quitting automatically - I just can't figure out which method to call to do so :(
-
Thanks for the info. I wrote the app myself, and know *when* I should be quitting automatically - I just can't figure out which method to call to do so :(
OK, I read the original post wrong. Application.Exit() is what you should be using. If it's not working, then you've got something else wrong in your app that is preventing your app from closing. How do you have your app processing command line arguments and printing your document? Are you doing this in the forms Load event? IIRC, calling Application.Exit() in the Load event doesn't do anything.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
OK, I read the original post wrong. Application.Exit() is what you should be using. If it's not working, then you've got something else wrong in your app that is preventing your app from closing. How do you have your app processing command line arguments and printing your document? Are you doing this in the forms Load event? IIRC, calling Application.Exit() in the Load event doesn't do anything.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks for your hint. During the form constructor, I detect if I have a command line argument listing the sheets to print and immediately proceed to handle it. This occurs before the main window is shown. I now set a flag that is tested during the form_Shown() handler and if I have printed sheets I call Application.Exit() as you recommended. Works like a charm :) Again, thanks! -Frank
-
Thanks for your hint. During the form constructor, I detect if I have a command line argument listing the sheets to print and immediately proceed to handle it. This occurs before the main window is shown. I now set a flag that is tested during the form_Shown() handler and if I have printed sheets I call Application.Exit() as you recommended. Works like a charm :) Again, thanks! -Frank
OK - it's kind of the wacky way to go about it, but whatever works. I'd normally create my own Main method, do whatever command line stuff I need to there and if I need to start the Windows Form app, branch off to the Application.Run(new MyMainForm). Other than that, let the Main method die normally.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...