TaskManagerClosing won't trigger RRS feed

  • Question

  • Howdy

    On MainForm_FormClosing I check If e.CloseReason = CloseReason.TaskManagerClosing Then...

    But however, when I run the app and try to close it via Task Manager, it's never fired, I always get CloseReason.UserClosing even if I close the app via Task Manager!

    Bug or my bad?!

    Friday, November 8, 2019 7:20 PM

All replies

  • I just had to laugh a little bit. Who does trigger the Task Manager. A user or somebody from heaven or whatever, not a user



    Friday, November 8, 2019 7:38 PM
  • I just had to laugh a little bit. Who does trigger the Task Manager. A user or somebody from heaven or whatever, not a user



    Thanks for input josh! Considering your logic, what's the difference between TaskManagerClosing and UserClosing? To fire TaskManagerClosing you ask Jesus to trigger it?!

    Or how to detect a user from heaven or hell (does not matter) is closing the app via Task Manager?!

    * I found it's a change since Windows 8, before that it was working as expected! Perhaps since Windows 8/8.1 some developer @ MS thought even TaskManagerClosing is fired by a UserClosing so found TaskManagerClosing useless!

    Should look for alternative way!

    • Edited by OSVBNET Saturday, November 9, 2019 10:56 AM
    Saturday, November 9, 2019 9:51 AM
  • For what its worth on Win 8.1 I used a minimal application to send a WM_CLOSE message to the main window of a VB.Net Windows Forms application and the CloseReason was set to TaskManagerClosing.  Then I sent a WM_SYSCOMMAND message with SC_CLOSE to the Winform app and it reported UserClosing.

    Perhaps they changed the way that Task Manger closes applications between Wndows versions.

    Saturday, November 9, 2019 1:43 PM
  • On Windows 10,
    Task Manager sends WM_SYSCOMMAND with SC_CLOSE
    and .NET sets on SC_CLOSE : CloseReason = CloseReason.UserClosing;

    So you could do something like that :

        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
        <DllImport("User32.dll", SetLastError:=True)>
        Public Shared Function GetForegroundWindow() As IntPtr
        End Function
        Public Const WM_SYSCOMMAND As Integer = &H112
        Public Const SC_CLOSE = &HF060
        Protected Overrides Sub WndProc(ByRef m As Message)
            If (m.Msg = WM_SYSCOMMAND AndAlso CInt(m.WParam) = SC_CLOSE) Then
                If (GetForegroundWindow() = FindWindow("TaskManagerWindow", Nothing)) Then
                    Console.Beep(1000, 100)
                End If           
            End If
        End Sub

    Saturday, November 9, 2019 2:43 PM
  • Win 8.1 Task Manager also sends WM_SYSCOMMAND with SC_CLOSE.

    Win 7 Task Manager sends WM_CLOSE.

    • Edited by RLWA32 Saturday, November 9, 2019 5:02 PM typo
    Saturday, November 9, 2019 3:42 PM