Re: How do I intercept a close event?

From: Branimir Giurov [C# MVP] (branimir_at_online.nospam)
Date: 01/13/05


Date: Thu, 13 Jan 2005 04:49:42 -0800

Hi Tinius,

I think that the easiest way of capturing the close event of the form, is
in fact to capture the WM_CLOSE message that's being send to the form through
the message loop of the OS. Here's an easy way for doing that

//insert the following code inside your main form class:

const int WM_CLOSE = 16;

protected override void WndProc(ref Message m)
{
        base.WndProc (ref m);

        if ( WM_CLOSE == m.Msg )
        {
                //closing
                Trace.WriteLine(Application.ExecutablePath + " is exiting !!!");
        }
}

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org

> I cannot get Environment.HasShutdownStarted to work.
> I placed it in;
> private void MainForm_Closing(object sender,
> System.ComponentModel.CancelEventArgs e)
> but it always returns false, even when shutting down WindowsXP prof.
>
> I placed this MessageBox in the same routine;
> MessageBox.Show(Environment.HasShutdownStarted.ToString(),"Shutting
> Down?"); which always returns FALSE.
>
> I'm at a loss with how to differentiate between the User closing the
> app down and Windows closing the app down
>
> I even wrote a second very simple app and placed the
> Environment.HasShutdownStarted in the form1_closing event. Still
> always returns false.
>
> I must be missing something obvious?
>
> Tinius
>
> "Greg Bacchus" wrote:
>
>> One note on that though. You should first check if the computer is
>> being shut down, otherwise the form will not automatically be closed
>> and the computer will not turn off.
>>
>> private void MainForm_Closing(object sender,
>> System.ComponentModel.CancelEventArgs e)
>> {
>> if( !Environment.HasShutdownStarted )
>> {
>> e.Cancel = true;
>> this.Hide();
>> }
>> }



Relevant Pages

  • Re: Determining error context
    ... add in the error context identification, ... Capturing the context did not seem particularly important until I started ... developing a non-interactive app. ... hard-code that into the call to your error logger. ...
    (microsoft.public.access.modulesdaovba)
  • Re: How do I intercept a close event?
    ... I tried what you suggested and its works fine for detecting when the User ... the App is closed via Shutdown. ... > I think that the easiest way of capturing the close event of the form, ... > Branimir Giurov ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: FileSystemWatcher WinXP versus Win2003
    ... still in used by the originator). ... my app picks it up. ... But i am capturing the Created and Changed events using: ... The copy does not work on the Win2003 but when i use notepad to save the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Window moving
    ... >capturing when the other app is moved/repositioned (please note it is not an ... >app I developed). ... Very little overhead, very little burden and ...
    (borland.public.delphi.nativeapi)
  • FileSystemWatcher WinXP versus Win2003
    ... my app picks it up. ... But i am capturing the Created and Changed events using: ... The copy does not work on the Win2003 but when i use notepad to save the ...
    (microsoft.public.dotnet.languages.csharp)