Threading.Timer & System Shutdown Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sin Jeong-hun

    Threading.Timer & System Shutdown Problem

    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.Dispos e() <-TheTimer is a Threading.Timer
    Application.Exi tThreads()
    Application.Exi t()

    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.

  • chanmm

    #2
    Re: Threading.Timer &amp; System Shutdown Problem

    Can you post you code so someone here can perhaps easy to try it for you?

    chanmm

    "Sin Jeong-hun" <typingcat@gmai l.comwrote in message
    news:1161917186 .370880.295090@ k70g2000cwa.goo glegroups.com.. .
    >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.Dispos e() <-TheTimer is a Threading.Timer
    Application.Exi tThreads()
    Application.Exi t()
    >
    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.
    >

    Comment

    • Sin Jeong-hun

      #3
      Re: Threading.Timer &amp; System Shutdown Problem

      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.Dispos e();
      TheNotifier.Clo se();
      TheNotifier.Dis pose();
      m.Result = IntPtr.Zero;
      Application.Exi tThread();
      Shutdown.DoExit Win(Shutdown.EW X_SHUTDOWN);
      }
      else
      {
      base.WndProc(re f m);
      }
      }

      chanmm wrote:
      Can you post you code so someone here can perhaps easy to try it for you?
      >
      chanmm
      >
      "Sin Jeong-hun" <typingcat@gmai l.comwrote in message
      news:1161917186 .370880.295090@ k70g2000cwa.goo glegroups.com.. .
      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.Dispos e() <-TheTimer is a Threading.Timer
      Application.Exi tThreads()
      Application.Exi t()

      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.

      Comment

      Working...