By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

Windows shutdown hanging - Getting desperate!

P: n/a
I have an application running in the background, visible only by a notify
icon in the system tray. I learned from other posts to catch the
WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I want to
shutdown/restart the computer the application exits but the shutdown process
does not continue anymore. Here's what I've done:

protected override void WndProc(ref Message m)
{
// Exit message code.
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
{
base.WndProc(ref m);
this.Close();
}
else
{
base.WndProc(ref m);
}
}

The MainForm is started in normal state and is set to invisible after start.
PLEASE HELP ME!
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David,

You need to set the return value when the message is WM_QUERYENDSESSION.
Basically, your code should look like this:

// Process the message normally.
base.WndProc(ref m);

// If the result is to query to end the session, set the return value to 1.
if (m.Msg == WM_QUERYENDSESSION)
{
// Return true for the result.
m.Result = 1;
}
else if (m.Msg == WM_ENDSESSION)
{
// Close the form.
this.Close();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
I have an application running in the background, visible only by a notify
icon in the system tray. I learned from other posts to catch the
WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I want to
shutdown/restart the computer the application exits but the shutdown
process
does not continue anymore. Here's what I've done:

protected override void WndProc(ref Message m)
{
// Exit message code.
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
{
base.WndProc(ref m);
this.Close();
}
else
{
base.WndProc(ref m);
}
}

The MainForm is started in normal state and is set to invisible after
start.
PLEASE HELP ME!

Nov 16 '05 #2

P: n/a
Hi Nicholas,

thanx a lot for your reply. This is an annoying little big problem! I
reworked the WndProc Method to this:

protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
// Carry on with the message.
base.WndProc(ref m);
if (m.Msg == WM_QUERYENDSESSION)
{
m.Result = (IntPtr) 1;
}
else if (m.Msg == WM_ENDSESSION)
{
this.Close();
}
}

However, it does not change anything. The application closes but shutdown
aborts as well. I noticed that the WM_QUERYENDSESSION message never triggers,
only the WM_ENDSESSION does. I am closing the applications log file (not
shown in the code above) before issuing "this.Close()" and therefore I see
what triggers the applications exit. I guess this does not make a difference,
does it?

Dave.

"Nicholas Paldino [.NET/C# MVP]" wrote:
David,

You need to set the return value when the message is WM_QUERYENDSESSION.
Basically, your code should look like this:

// Process the message normally.
base.WndProc(ref m);

// If the result is to query to end the session, set the return value to 1.
if (m.Msg == WM_QUERYENDSESSION)
{
// Return true for the result.
m.Result = 1;
}
else if (m.Msg == WM_ENDSESSION)
{
// Close the form.
this.Close();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
I have an application running in the background, visible only by a notify
icon in the system tray. I learned from other posts to catch the
WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I want to
shutdown/restart the computer the application exits but the shutdown
process
does not continue anymore. Here's what I've done:

protected override void WndProc(ref Message m)
{
// Exit message code.
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
{
base.WndProc(ref m);
this.Close();
}
else
{
base.WndProc(ref m);
}
}

The MainForm is started in normal state and is set to invisible after
start.
PLEASE HELP ME!


Nov 16 '05 #3

P: n/a
David,

The only other thing that you should be sure to do is set the Result
property of the message to 0 when the message is WM_ENDSESSION. This
indicates that it is ok to shut down.

It ^might^ be an issue that you have the log file open. Does calling
close on the application actually close the app? Can you use
Application.Exit instead to shut down?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
Hi Nicholas,

thanx a lot for your reply. This is an annoying little big problem! I
reworked the WndProc Method to this:

protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
// Carry on with the message.
base.WndProc(ref m);
if (m.Msg == WM_QUERYENDSESSION)
{
m.Result = (IntPtr) 1;
}
else if (m.Msg == WM_ENDSESSION)
{
this.Close();
}
}

However, it does not change anything. The application closes but shutdown
aborts as well. I noticed that the WM_QUERYENDSESSION message never
triggers,
only the WM_ENDSESSION does. I am closing the applications log file (not
shown in the code above) before issuing "this.Close()" and therefore I see
what triggers the applications exit. I guess this does not make a
difference,
does it?

Dave.

"Nicholas Paldino [.NET/C# MVP]" wrote:
David,

You need to set the return value when the message is
WM_QUERYENDSESSION.
Basically, your code should look like this:

// Process the message normally.
base.WndProc(ref m);

// If the result is to query to end the session, set the return value to
1.
if (m.Msg == WM_QUERYENDSESSION)
{
// Return true for the result.
m.Result = 1;
}
else if (m.Msg == WM_ENDSESSION)
{
// Close the form.
this.Close();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
>I have an application running in the background, visible only by a
>notify
> icon in the system tray. I learned from other posts to catch the
> WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I want to
> shutdown/restart the computer the application exits but the shutdown
> process
> does not continue anymore. Here's what I've done:
>
> protected override void WndProc(ref Message m)
> {
> // Exit message code.
> int WM_QUERYENDSESSION = 0x11;
> int WM_ENDSESSION = 0x16;
> if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
> {
> base.WndProc(ref m);
> this.Close();
> }
> else
> {
> base.WndProc(ref m);
> }
> }
>
> The MainForm is started in normal state and is set to invisible after
> start.
> PLEASE HELP ME!


Nov 16 '05 #4

P: n/a
Hi Niclas, you are a mastermind! It seems to work now when putting the
0-return value in the code. I just wonder why other people got it to work
without both of the return values... Thanks a LOT! Dave.

"Nicholas Paldino [.NET/C# MVP]" wrote:
David,

The only other thing that you should be sure to do is set the Result
property of the message to 0 when the message is WM_ENDSESSION. This
indicates that it is ok to shut down.

It ^might^ be an issue that you have the log file open. Does calling
close on the application actually close the app? Can you use
Application.Exit instead to shut down?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
Hi Nicholas,

thanx a lot for your reply. This is an annoying little big problem! I
reworked the WndProc Method to this:

protected override void WndProc(ref Message m)
{
int WM_QUERYENDSESSION = 0x11;
int WM_ENDSESSION = 0x16;
// Carry on with the message.
base.WndProc(ref m);
if (m.Msg == WM_QUERYENDSESSION)
{
m.Result = (IntPtr) 1;
}
else if (m.Msg == WM_ENDSESSION)
{
this.Close();
}
}

However, it does not change anything. The application closes but shutdown
aborts as well. I noticed that the WM_QUERYENDSESSION message never
triggers,
only the WM_ENDSESSION does. I am closing the applications log file (not
shown in the code above) before issuing "this.Close()" and therefore I see
what triggers the applications exit. I guess this does not make a
difference,
does it?

Dave.

"Nicholas Paldino [.NET/C# MVP]" wrote:
David,

You need to set the return value when the message is
WM_QUERYENDSESSION.
Basically, your code should look like this:

// Process the message normally.
base.WndProc(ref m);

// If the result is to query to end the session, set the return value to
1.
if (m.Msg == WM_QUERYENDSESSION)
{
// Return true for the result.
m.Result = 1;
}
else if (m.Msg == WM_ENDSESSION)
{
// Close the form.
this.Close();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"David Meier" <Da********@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
>I have an application running in the background, visible only by a
>notify
> icon in the system tray. I learned from other posts to catch the
> WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I want to
> shutdown/restart the computer the application exits but the shutdown
> process
> does not continue anymore. Here's what I've done:
>
> protected override void WndProc(ref Message m)
> {
> // Exit message code.
> int WM_QUERYENDSESSION = 0x11;
> int WM_ENDSESSION = 0x16;
> if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
> {
> base.WndProc(ref m);
> this.Close();
> }
> else
> {
> base.WndProc(ref m);
> }
> }
>
> The MainForm is started in normal state and is set to invisible after
> start.
> PLEASE HELP ME!


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.