2

How to write a WinForms application that would not have any visible element except from a tray icon and at the same time allow the application to be closed by Windows Installer if the file is in use? As I understand it the Windows Installer sends a WM_CLOSE message to application's main window. I assume that the "main window" is indicated by MainForm property of ApplicationContext. Also it seems to me that for this mechanism to work the form must have been shown at least once.

I found some workarounds like creating an empty form and setting its opacity to 0 (to minimize flicker) then showing and hiding, but describing this as a nasty hack would heavily underestimate its ugliness.

Tomasz Grobelny
  • 2,666
  • 3
  • 33
  • 43
  • Did you really tried with a form with visible=false and it did not work? did you try to debug to see if the FormClosing event was fired? – Gusman Apr 22 '14 at 22:17
  • possible duplicate of [How to start WinForm app minimized to tray?](http://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray) – Hans Passant Apr 23 '14 at 17:07
  • You do need a hidden window otherwise you can't receive the message. In fact if you look at the NotifyIcon class it will create a hidden NativeWindow for you if you don't supply one although it won't close the application on WM_CLOSE or WM_ENDSESSION. – Tony Edgecombe Nov 10 '16 at 11:21

1 Answers1

0

Windows Installer does not send any WM_CLOSE messages. This might help:

http://haishibai.blogspot.com/2010/02/complete-tutorial-patch-restart-your.html

It's a slightly different scenario but might get you started if you want Windows Installer to tell your app when it should close down.

On recent operating system versions with Restart Manager a tray app will be detected. Back on Windows XP it wasn't detected.

In the WiX world there is a CloseApplication utility custom action that will send WM_CLOSE to the app window.

PhilDW
  • 20,260
  • 1
  • 18
  • 28