question

sunilmarulasiddaiah-7880 avatar image
2 Votes"
sunilmarulasiddaiah-7880 asked HuiLiu-MSFT commented

I need some method/event to be fired when i close my application from task manager.

Hi Team,

I have the scenario like I have WPF application running on my machine with only one window.

I am making that window visibility hidden when the window got loaded.

Now have senario to fire the event/method to check the process/window got closed from task manager.
227959-image.png

Best Regards,
Sunil


dotnet-csharpwindows-wpf
image.png (34.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is a good question. I tried for several hours using API functions like WM_CLOSE (if I'm not mistaken, Windows 8.1 used another function to end processes) to answer this question, but I couldn't.
However, I believe it's possible.

0 Votes 0 ·

Hi,@sunilmarulasiddaiah-7880. Is there any update to this question? Did these answers solve your problem? If so, you could accept it as an answer. It is helpful to community members with related questions.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered sunilmarulasiddaiah-7880 commented

You will find the solution here.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HI,

The above solution did not work for wpf application.

In my case window will be visibility hidden, but it will running and i could see the same app running in task manager.

if i kill the process from task manager / if i click on stop debugging it should hit break point in my wpf saying that its getting closed..

Best Regards,
Sunil

0 Votes 0 ·
HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered

Hi,@sunilmarulasiddaiah-7880. You could use the following code to prompt the application to close with an empty window.
Codebehind:

    private void Window_Loaded(object sender, RoutedEventArgs e)
         {
             this.Visibility = Visibility.Hidden;
             Window wnd = new Window();
             wnd.Width = 0;
             wnd.Height = 0;
             wnd.WindowStyle = WindowStyle.None;
    
             wnd.Loaded += delegate
             {
                 HwndSource source = (HwndSource)PresentationSource.FromDependencyObject(wnd);
               //  wnd.Visibility = Visibility.Hidden;
                 source.AddHook(WindowProc);
    
             };
    
             wnd.Show();
         }


    private static IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
         {
    
             switch (msg)
    
             {
    
                 case 0x11:
    
                 case 0x16:
                     MessageBox.Show("Close reason: WindowsShutDown");
                     
                     break;
    
                 case 0x112:
    
                     if ((LOWORD((int)wParam) & 0xfff0) == 0xf060)
                     {
                         MessageBox.Show("Close reason: UserClosing");
                          
                     }
    
                     break;
    
             }
    
             return IntPtr.Zero;
    
         }


If the response is helpful, please click "Accept Answer" and upvote it.
 Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 edited

I created a WPF App (.Net Framework 4.8) using the Window_Loaded event to hide the window as shown above. Following is the process window tree shown by Spy++. The MainWindow is not visible and Spy++ is set to log all window messages that MainWindow receives.

228542-wpfbefore.png

Then I used Task Manager to End Task on WPFApp1.exe. The application closed but no window messages were seen by Spy++. It seems to me that Task Manager used TerminateProcess instead of sending a WM_SYSCOMMAND (SC_CLOSE) or WM_CLOSE message to the hidden window.

For comparison, this is what Spy++ sees when MainWindow is not hidden and the process is closed by Task Manager -

228507-wpfvisible.png

Consider a different way to allow a user to end the hidden window application -- use a notification area icon to allow the user to close the application instead of Task Manager.



wpfbefore.png (71.4 KiB)
wpfvisible.png (96.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.