RKC

Hi All,

If my application is running and shutdown process for windows has started then my application might be receiving exit command from OS. I want to cathc this command and want to perform some activities.

How to do this

Thanks and Regards,

Ravi




Re: Windows Forms General How to handle exit command given by OS in my aaplication?

Bugs

have you try FormClose or FormClosing event

if there is no c# function to go more over to catch windows event, you will have to include c++ syscommand event...





Re: Windows Forms General How to handle exit command given by OS in my aaplication?

Alberto Poblacion

You can connect an event handler to the static event ApplicationExit of the Application class.





Re: Windows Forms General How to handle exit command given by OS in my aaplication?

RKC

I'm using below given code.

protected override void WndProc(ref Message m)

{

base.WndProc(ref m); // pass message on to base form

//Handling the WM_QUERYENDSESSION AND WM_ENDSESSION

int WM_QUERYENDSESSION = 0x11;

int WM_ENDSESSION = 0x16;

if (m.Msg == WM_QUERYENDSESSION)

{

m.Result = (IntPtr)1;

}

else if (m.Msg == WM_ENDSESSION)

{

m.Result = (IntPtr)0;

//SomeTask ();

this.Close();

}

}

This code close my application if shutdownfor computer is initiated but never processed further. One thing i noticed is that it never goes into WM_QUWERENDSESSION block.

Tell me where i'm wrong.

Ravi






Re: Windows Forms General How to handle exit command given by OS in my aaplication?

IsshouFuuraibou

Two things, one perform the base.WndProc(ref m) call at the END of the function. Another application might be receiving and clearing the message.

The second thing, you need to make sure the message is for your application, otherwise you'll process every single message event if the message isn't directed to you. I'm assuming it will send multiple messages, or send one without a windows handle specifier.

Now I would not recommend the WndProc method of handling the shutdown case.
It would be far better to deal with Application.ApplicationExit event:
Application.ApplicationExit
With the Environment.HasShutdownStarted property:
Environment.HasShutdownStarted
that way you're not dealing with needing to handle windows messages (which gets processed for EVERY windows message not just exiting/shutdown) and not have to worry about whether you need to clear the message or not.





Re: Windows Forms General How to handle exit command given by OS in my aaplication?

nobugz

Just use the FormClosing event in your main startup form, look for e.CloseReason == CloseReason.WindowsShutdown. SystemEvents.SessionEnding is another approach. Note the remark about WM_QUERYENDSESSION in the MSDN article for that event.





Re: Windows Forms General How to handle exit command given by OS in my aaplication?

Cool_Zer0

Hi.

I'm having the same problem.

I've made a simple application and everything seems fine (I receive a WM_QUERYENDSESSION and then a WM_CLOSE).
On my application I'm only receiving a WM_ENDSESSION with wParam=0 (which means that no close will occur).

This is my code:
Code Block

protected override void WndProc(ref Message m)
{
System.IO.TextWriter tw = new System.IO.StreamWriter("c:\\baseform.txt", true);
tw.WriteLine(m);

// If the result is to query to end the session, set the return value to 1.
if (m.Msg == 0x11)
{
// Allow to quit the application.
m.Result = (IntPtr) 1;
tw.WriteLine("m.Result=1");
}
else if (m.Msg == 0x16 && m.WParam == (IntPtr) 1)
{
// Close the form.
m.Result = (IntPtr) 0;
tw.WriteLine("m.Result=0");
Application.Exit();
return;
}

tw.WriteLine("\t\tContinuing!");
tw.Close();

// Call the base.
base.WndProc(ref m);

}


An excert of the log is:
msg=0x6 (WM_ACTIVATE) hwnd=0x18053c wparam=0x0 lparam=0x1904e2 result=0x0
Continuing!
msg=0x111 (WM_COMMAND) hwnd=0x18053c wparam=0x20004e0 lparam=0x2604e0 result=0x0
Continuing!
msg=0x16 (WM_ENDSESSION) hwnd=0x18053c wparam=0x0 lparam=0xffffffff80000000 result=0x0
Continuing!
msg=0x16 (WM_ENDSESSION) hwnd=0x1a0450 wparam=0x0 lparam=0xffffffff80000000 result=0x0
Continuing!
msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x18053c wparam=0x5a010992 lparam=0x2604e0 result=0x0
Continuing!
msg=0x133 (WM_CTLCOLOREDIT) hwnd=0x18053c wparam=0x5a010992 lparam=0x2604e0 result=0x0
Continuing!

I can see the END_SESSION but no QUERYENDSESSION is being received...

Any hint on what the problem may be

Thanks a lot