Threading.Timer & System Shutdown Problem

  • Thread starter Thread starter Sin Jeong-hun
  • Start date Start date
S

Sin Jeong-hun

I created a windows form application. It has a Threading.Timer and when
the timer ticks it does some work and show a popup window.

The problem is that while this program is running if the user tries to
shutdown Windows, my application doesn't quit and neither does
Windows. Of course, if the user first click [X] of my application and
then tries to shutdown, there is no problem.

To solve this problem, I overrode WndProc of the main form, and add
some code like
IF the message = WM_ENDSESSION
Message.Result = IntPtr.Zero <-MSDN says that I should return 0
TheTimer.Dispose() <-TheTimer is a Threading.Timer
Application.ExitThreads()
Application.Exit()

Now when tried to shutdown, my application quits but Windows doesn't.
As a workaround I added the following precedures like
...
ExitWindows() <--This shutsdown Windows

It seems working but the problem is that I cannot know whether
WM_ENDSESSION is fired by Shutdown or Restart. MSDN says "it is not
possible to determine which event is occurring". So currently, if the
user Restart while my application is running, Windows will just
shutdown.

Do you see what the problem is? Why doesn't Windows shutdown even
though I returned 0 to the WM_ENDSESSION message and terminated my
application? If you have any idea, please let me know. Thank you.
 
I could, but maybe listing all the source code would be confusing to
the readers. That's why I just explained my code in psudocode. Anyways,
here is the real code.

public partial class MainForm : Form
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0016)
{
StopTimer();
TheTimer.Dispose();
TheNotifier.Close();
TheNotifier.Dispose();
m.Result = IntPtr.Zero;
Application.ExitThread();
Shutdown.DoExitWin(Shutdown.EWX_SHUTDOWN);
}
else
{
base.WndProc(ref m);
}
}
Can you post you code so someone here can perhaps easy to try it for you?

chanmm

Sin Jeong-hun said:
I created a windows form application. It has a Threading.Timer and when
the timer ticks it does some work and show a popup window.

The problem is that while this program is running if the user tries to
shutdown Windows, my application doesn't quit and neither does
Windows. Of course, if the user first click [X] of my application and
then tries to shutdown, there is no problem.

To solve this problem, I overrode WndProc of the main form, and add
some code like
IF the message = WM_ENDSESSION
Message.Result = IntPtr.Zero <-MSDN says that I should return 0
TheTimer.Dispose() <-TheTimer is a Threading.Timer
Application.ExitThreads()
Application.Exit()

Now when tried to shutdown, my application quits but Windows doesn't.
As a workaround I added the following precedures like
...
ExitWindows() <--This shutsdown Windows

It seems working but the problem is that I cannot know whether
WM_ENDSESSION is fired by Shutdown or Restart. MSDN says "it is not
possible to determine which event is occurring". So currently, if the
user Restart while my application is running, Windows will just
shutdown.

Do you see what the problem is? Why doesn't Windows shutdown even
though I returned 0 to the WM_ENDSESSION message and terminated my
application? If you have any idea, please let me know. Thank you.
 
Back
Top