locked
Run Code when Console Application Exits RRS feed

  • Question

  • Basically I have some code I want to run whenever my console application exits, whatever happens. Whether I hit the X, the computer shuts down, whatever. Is this possible? There's no "FormClosing" for console apps, so I can't quite figure out how to do this.
    Thursday, May 20, 2010 11:29 PM

Answers

  • First, I don't know if it is impossible to do this within a console application.  Since as a service or as a windows forms application it is possible, then why are you forcing the console application requirement?

    --
    Mike
    Friday, May 21, 2010 12:52 AM
  • I ended up just making it a windows forms application.
    Sunday, May 23, 2010 3:01 AM

All replies

  • First, I don't know if it is impossible to do this within a console application.  Since as a service or as a windows forms application it is possible, then why are you forcing the console application requirement?

    --
    Mike
    Friday, May 21, 2010 12:52 AM
  • Right now it's a console application. I don't know if I want to change it entirely for this one requirement. Basically it uses outlook, and every time I shut down outlook freezes up and stops it until I click OK to kill it. I was hoping I could make my application kill outlook on exit.
    Friday, May 21, 2010 1:24 AM

  • I suspect that it may be easier, and it is certainly better, to solve why Outlook is frozen.

    --
    Mike
    Friday, May 21, 2010 1:37 AM
  • I'm using outlook connector to check my hotmail account for new emails. So I'm hitting send/receive every 10 seconds or so. I think that's what's doing it, but I don't want to change that.

    Friday, May 21, 2010 1:20 PM
  • I ended up just making it a windows forms application.
    Sunday, May 23, 2010 3:01 AM
  • Why don't you create a new, empty "Windows Application" type project, but DON'T add any forms. Write your own Main(...) method. Then launch your console application as a process, and clean up when the process terminates. Simple, effective, works...
    Sunday, May 23, 2010 9:53 AM
  • Why don't you create a new, empty "Windows Application" type project, but DON'T add any forms. Write your own Main(...) method. Then launch your console application as a process, and clean up when the process terminates. Simple, effective, works...

    But if shutdown kills the windows application before the console application, it won't be able to clean up.
    Tuesday, May 25, 2010 2:26 PM
  • If you launch a process inside the "empty" Windows application and the Windows app shuts down, so do its child processes (and any "cleanup" code you write will be able to execute). If the console application is aborted, then the "parent" Windows application has no reason to continue executing. So that's not going to happen. You have several options available. You can use IDisposable with the RAII pattern to make things easy. I'm sure you're familiar with try/finally pattern as well. You write your code so that no matter how things end up, your cleanup code is going to execute. It's rather common to need a piece of code to execute when an application is closing, and this is just one very simple way to do it.
    Thursday, June 3, 2010 10:21 AM