Detecting Windows shutdown

  • Thread starter Thread starter Dave Townsend
  • Start date Start date
D

Dave Townsend

I am trying to write an application that starts with windows and sits in
the systray, only showing a form when the user clicks the systray icon.
I have got it working almost perfectly. The systray icon shows up, right
clicking it and selecting exit exits the application.

My problem is that when I shutdown windows while the application is
running it just stops. The Application.ApplicationExit event is never
raised. I have some settings that need to be persisted and resources
freed when the application exits so somehow I need to detect when
windows is closing my application and respond accordingly.

Is there any sensible way to do this in .NET?

Dave
 
* Dave Townsend said:
I am trying to write an application that starts with windows and sits in
the systray, only showing a form when the user clicks the systray icon.
I have got it working almost perfectly. The systray icon shows up, right
clicking it and selecting exit exits the application.

My problem is that when I shutdown windows while the application is
running it just stops. The Application.ApplicationExit event is never
raised. I have some settings that need to be persisted and resources
freed when the application exits so somehow I need to detect when
windows is closing my application and respond accordingly.

<http://www.mvps.org/dotnet/dotnet/samples/windowsandforms/downloads/CloseWindow.zip>
 
Well I was rather hoping for something a little more elegant, but still.

I tried the code below with no success. I also tried writing a data file
or writing to the registry instead of the message box but with no
results. I'm guessing though that since the form is not visible, it will
never receive any messages?

Dave

protected override void WndProc(ref Message m)
{
int WM_ENDSESSION = 0x16;

if (m.Msg==WM_ENDSESSION)
{
MessageBox.Show("Windows closing");
}
base.WndProc(ref m);
}
 
Dave,

I wrote this sometime back. It might help with what you're
trying to do. Note the WM_* messages.

Hope this helps.
Dave

/// <summary>
/// This function is implemented so that the Close event
/// can be previewed before it is handled as a normal MDI
/// close event.
/// If this function were not implemented, the MDI child
/// forms would see the Closing event prior to the MDI
/// parent form. Child forms would then have no way to
/// distinguish child close requests from parent close
/// requests.
/// </summary>
/// <param name="m">The Message being processed.</param>

protected override void WndProc(ref Message m)
{
// First test for the user clicking the Close box (X).
if ( m.Msg == WM_SYSCOMMAND
&& m.WParam == (IntPtr)SC_CLOSE )
{
appClosing = true;
}
// Next, test for Windows trying to shutdown.
else if ( m.Msg == WM_QUERYENDSESSION
|| m.Msg == WM_ENDSESSION )
{
appClosing = true;
}
base.WndProc(ref m);
}
 
FOr the WM_SYSCOMMAND, SC_CLOSE you can use the Cancellable message on close
event
just set e.Cancel = true.

Why use IntPtr cast for Wparam? wParam.Int32() works :D
 
This is something someone else suggested and I have tried, but could not
get to work. I think that since the form is hidden (in fact in general
it will never have been shown) the window doesn't receive windows
messages.

Any other ways to get this to work?

Dave
 
Is it entering WndProc? Can you see what messages the window is getting
using Spy++ ?
 
As far as I can tell WndProc is never being called. I also cannot see
the hidden form in Spy++. I have worked round this by showing the form
at a position that is off the desktop, but this is hardly the best way
to go about it.

Any suggestions?

Dave
 
Back
Top