title: Refreshing system tray icons programmatically
body: <p>I've an application which has a system tray icon. While uninstalling I'm killing the process if its running. So, as am not gracefully stopping the app, the icon remains in the system tray and will remove only if we hover the mouse on it. I wrote a code that would run the cursor along the tray and get the cursor back in its initial position. This is what I have done:</p>
  
  <pre><code>        [DllImport("user32.dll")]
          static extern IntPtr FindWindow(string className, string windowName);
          [DllImport("user32.dll")]
          static extern IntPtr FindWindowEx(IntPtr parent, IntPtr child, string className, string windowName);
          [DllImport("user32.dll")]
          static extern bool GetWindowRect(HandleRef handle, out RECT rct);
  
          [StructLayout(LayoutKind.Sequential)]
          struct RECT
          {
              public int Left;
              public int Top;
              public int Right;
              public int Bottom;
          }
  
          void RefreshTray()
          {
              IntPtr taskbar_Handle = FindWindow("Shell_Traywnd", "");
              IntPtr tray_Handle = FindWindowEx(taskbar_Handle, IntPtr.Zero, "TrayNotifyWnd", "");
  
              RECT rct;
  
              if (!(GetWindowRect(new HandleRef(null, tray_Handle), out rct)))
              {
              }
  
              System.Drawing.Point init = Control.MousePosition;
  
              for (int i = rct.Left; i &lt; rct.Right-20; i++)
              {
                  Cursor.Position = new System.Drawing.Point(i, (rct.Bottom + rct.Top) / 2);
              }
  
              Cursor.Position = init;
           }
  </code></pre>
  
  <p>This works good in all the cases except when the option "do not show notification icons" is enabled. Is there some way I could refresh the tray in this case?</p>
  
  <p><strong>EDIT</strong>
  As the comments suggested I changed my approach. Instead of killing the tray application, I established a communication between my application service (yeah, forgot to mention, I have a service too running along with the application) and tray application. While uninstalling, I stop the service, from the service stop method I would send a socket message of a particular format to the tray application and ask it to close and I would set the notify icon visibility to false. This would leave the Tray Application running in background so I am using "taskkill" to remove the application. It worked fine in Win7 and Vista, but is not working properly in Win XP. But I have not written any environment specific code. Any possible clue?</p>