1

I have a WPF app that runs in the System Tray. It is based on the NotifyIcon project (http://www.codeproject.com/Articles/36468/WPF-NotifyIcon).

I need to get it to respond to the WM_CLOSE command that is issued by the taskkill /IM command (that is run from the command line).

Does anyone know what method I need in my app to respond to WM_CLOSE ?

UPDATE

I should have made clear, for a system tray app, there might not be a window present.

I tried adding onClosing to the Window object. However if I issue taskkill /IM ... it only gets fired if the window is open.

public partial class MainWindow : Window
{

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        base.OnClosing(e);
    }

So I guess I need something on the application. For the application, I have tried overriding OnExit and the Exit event - but neither got triggered.

spiderplant0
  • 3,872
  • 12
  • 52
  • 91
  • Try this https://pingfu.net/csharp/2015/04/22/receive-wndproc-messages-in-wpf.html – Michael Z. May 27 '16 at 22:31
  • @Michael thanks for the suggestions - I have tried your suggestions but no luck - see updated question. – spiderplant0 May 28 '16 at 10:26
  • @M312V thanks for the suggestions - I have tried your suggestions but no luck - see updated question. – spiderplant0 May 28 '16 at 10:26
  • You may not need a window, but you certainly need a message loop. That message loop is where you'll receive the `WM_CLOSE` message. Presumably WPF has some idiomatic way of handling messages before they are dispatched, something akin to "PreTranslateMessage" or whatever. – Cody Gray - on strike May 28 '16 at 10:31
  • Maybe this is helpful : http://stackoverflow.com/questions/9826197/handle-wm-close-message-send-to-c-sharp-tray-app – Ton Plooij May 28 '16 at 10:52
  • 1
    There is *always* a window, only way that notifications can work. But it is well hidden in that project, it is WindowMessageSink. It is likely to be will hidden from taskkill.exe as well, you'll have to try. Task Manager used to use WM_CLOSE in the olden days. It doesn't any more, makes it behave too random. It now uses TerminateProcess() redrum, high odds that taskkill.exe does as well. – Hans Passant May 28 '16 at 10:55
  • @spiderplant0 Did you ever find a satisfactory solution? I am faced with a similar problem try to get a WiX installer to stop a system tray application based on the Hardcodet.Wpf.TaskbarNotification.dll. I understand that the WiX installer util:CloseApplication element possibly sends WM_CLOSE, so I would love to know how to catch it. – AngCaruso Jan 24 '17 at 21:48

0 Answers0