Need to kill a process gracefully

I developed an application that kills a program then restarts it. The only problem is that the system tray contains two icons (or more depending on how many times the applicaiton is killed then restarted). I need to figure out how to either: A) refresh the system tray B) close the program gracefully (the process close() and process closemainwindow() do no work) or C) figure out a way in C# to move the mouse over the system tray programacally to force the ghosted icon to disappear. PLEASE HELP!!!!

Thanks,

Jason


Answer this question

Need to kill a process gracefully

  • Gus B

    I think this is a natural thing/behaviour - sometimes Windows forgets to remove the item from the systray once the app is closed or could be because the application has not correctly removed its icon from systray so next time you hover over I think Windows will check to see if that app is still running/exists

    you can programmatically set the mouse position to that place where the Icon is but this would not be a good idea (different screen res, the systray icon could be in a different place, user wont be happy the fact that the mouse has moved automatically etc....)

    you can P/Invoke using Shell32.dll with a function/API called Shell_NotifyIcon

    http://www.pinvoke.net/default.aspx/shell32/Shell_NotifyIcon.html

    but not recommended to P/Invoke unless you REALLLY have to.

     

     



  • sagittarian

    That is windows problem and it's hard to solve. If application that you kill and restart is yours, than why not remove notifyIcon from that application and place in main application that kills and start application.

  • holy_spirit

    You might want to try sending the application a WM_QUIT message instead of a WM_CLOSE message. Some documentation seems to suggest that you should use PostMessage instead of SendMessage, but I'm not sure if this matters if the message is sent from another program.

  • Matt Stum

  • JohnWP

    How are you killing the app

    I have used Sendmessage with WM_CLOSE in the past without problems (little "genter" when closing an app)

    Most methods that "kill" an app...do so without giving the app a chance to "unload" or do cleanup.



  • yousaid

    I don't think this approach will solve the problem, I've seen the ghosted icons too when I simply remove the notification icon with the program still running. I think it is just a windows bug/limitation. One possible approach, and I haven't tried it, is to force that window to refresh itself with the Invalidate() Windows API call. To find the window, first look for the taskbar window itself with FindWindow("Shell_TrayWnd", ""), then find the tray with EnumChildWindows (class name = "TrayNotifyWnd"). A fair amount of work with no guaranteed success...


  • IreneSmith

    Sendmessage "WM_CLOSE" is a C++ command that is set to .Close(); in C#
    I have also tried .CloseMainWindow(); and that just minimises it to the systray. The application in a VPN program - I test to see if the system is on OUR network or not - then in order for it to enable or disable the VPN Firewall - I have to .KILL(); the app - thus lies the problem of the multiple icons.

  • Need to kill a process gracefully