Results 1 to 12 of 12

Thread: handle closing of the Concole window

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    handle closing of the Concole window

    I have Console application. How can I handle closing of the Concole window ?

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: handle closing of the Concole window

    what do you mean?
    If you're displaying text and it's closing before you can read it, then you'd just have to put a Console.Readline()
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: handle closing of the Concole window

    If you run your app in a console window that is already open, that window will remain open when your app exits. Likewise, if your app opened a new window in which to run that window will be closed when your app exits. In the second case, to avoid the window closing without warning the user, take MrPolite's advice, perhaps preceeded with Console.Write("Press ENTER to exit.").
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: handle closing of the Concole window

    I guess he/she meant the cmd.exe Window. He/She wants to check that the Windows is being closed explicitly by the user to avoid loss of information on his/her program running in console mode. I guess we don't have access to knowing this thing. Windows starts it as a thread.

    Or we can get the thread ID of this one and remember it when Windows tries to kill it?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    Re: handle closing of the Concole window

    Quote Originally Posted by MrPolite
    what do you mean?
    If you're displaying text and it's closing before you can read it, then you'd just have to put a Console.Readline()

    I have Console application. Sometimes my application prints something using Console.WriteLine. I want to handle closing of my application by user pressing
    on X at top right corner of the Console window. I want to execute any code before my application closes.

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: handle closing of the Concole window

    well the reason the window closes is because you application has finished. If you run cmd.exe and then run your program from there, you'll notice that the window wont close by itself (becuase when your program finishes running, it returns to cmd.exe and cmd.exe is still running).

    As suggested before, you can do a Console.ReadLine() at the end.
    (you could do that in a while-loop also )
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: handle closing of the Concole window

    As far as I'm aware you're console app has no actual awareness of the window it's running in. If the user closes a console window using that close button before the app has finished execution then, frankly, they're an idiot and deserve to lose their data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: handle closing of the Concole window

    they're an idiot and deserve to lose their data.
    That's why we say "All user's are loosers".
    You got to think of users as a bunch of overactive kids that will break anything that can be broken.
    One just have to keep Murphy's law in mind when developing applications.

    There must be a way one can attach to a window and receive some event notification as it is closed. Before it was done with API, I'm sure. I don't have it on my machine now, but look in APIViewer if you got it.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: handle closing of the Concole window

    *Ahem*

    use this event (fires shortly before a program exits)...

    Application.ApplicationExit()
    I don't live here any more.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: handle closing of the Concole window

    Quote Originally Posted by wossname
    *Ahem*

    use this event (fires shortly before a program exits)...

    Application.ApplicationExit()
    But the Application class is a member of the System.Windows.Forms namespace, so I'm not sure that it can be used in a console app. It may be possible, but I don't think it would be in that namespace if it was intended to be used for non WinForms apps.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: handle closing of the Concole window

    Hmm. Seems you're right about this but I know for a fact that you can use Application.StartUpPath from within a console window because I did it the other day.

    Probably because its static.

    Hmm (again).
    I don't live here any more.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: handle closing of the Concole window

    I had a quick test and you can add a reference to System.Windows.Forms to a console app with no problems, but the Application.ApplicationExit event doesn't get raised. I tried using AddHandler to attach a procedure to the event in both a WinForms app and a console app and the procedure was executed in WinForms but not the console. Presumably a WinForms app has a tighter connection to that namespace somehow.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width