title: NotifyIcon not responsing to changes in its ContextMenu
body: <p>My (formless system-tray only) program has a <code>NotifyIcon</code> whose <code>ContextMenu</code> value must change dynamically on the running course of the program. The notification icon object is instantiated when the program starts as follows:</p>
  
  <pre><code>notifyIcon = new NotifyIcon()
  {
      Icon = new Icon(@"C:\Users\basil\documents\visual studio 2015\Projects\Project Repository\Project Repository\Program Icon.ico"),
      Text = "My Program Name",
      Visible = false // Don't make it visible yet!
  };
  notifyIcon.ContextMenu = new ContextMenu();
  notifyIcon.ContextMenu.MenuItems.AddRange(new MenuItem[] { new MenuItem("Host Service") { Index = 0 }, new MenuItem("Backup Configurations", new EventHandler(delegate { bckConf.Show(); })) { Index = 1 }, new MenuItem("Network Info", new EventHandler(delegate { new form_NetworkInfo().Show(); })) { Index = 2 }, new MenuItem("About", new EventHandler(delegate { new form_AboutUs().Show(); })) { Index = 3 }, new MenuItem("Exit", new EventHandler(delegate { TerminateProgram(ProgramTerminateReason.UserRequest); })) { Index = 4 } }); 
  </code></pre>
  
  <p>In order to change the <code>ContextMenu</code> of the <code>NotifyIcon</code>, a public method named <code>ChangeNotifyIconState</code> is created as menu items need to be changed as per the results generated by the objects of other classes in the program whose declaration is as follows: (Do disregard the variables of the "CurrentServiceState" data-type and note that the <code>NotifyIcon</code> object is declared as a class variable)</p>
  
  <pre><code>static public void ChangeNotifyIconState(CurrentServiceState state)
  {
     notifyIcon.ContextMenu.MenuItems[0].MenuItems.Clear(); // First clear all menu items of the to-be-updated block
     if (state==CurrentServiceState.Running)
     { // Service has started running; disable the start button and activate the stop and restart buttons
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Start service", new EventHandler(delegate { HostServices.Start(); })) { Index = 0, Enabled = false });
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Stop service", new EventHandler(delegate { HostServices.Stop(); })) { Index = 0, Enabled = true });
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Restart service", new EventHandler(delegate { HostServices.Restart(); })) { Index = 0, Enabled = true });
     } else if (state==CurrentServiceState.Stopped)
     { // Service has stopped running; disable the stop and restart button and activate the start button
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Start service", new EventHandler(delegate { HostServices.Start(); })) { Index = 0, Enabled = true });
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Stop service", new EventHandler(delegate { HostServices.Stop(); })) { Index = 0, Enabled = false });
        notifyIcon.ContextMenu.MenuItems[0].MenuItems.Add(new MenuItem("Restart service", new EventHandler(delegate { HostServices.Restart(); })) { Index = 0, Enabled = false })};
     }
  }
  </code></pre>
  
  <p>Here's the problem: when I call <code>ChangeNotifyIconState(CurrentServiceState.Running)</code>, the required <code>MenuItems</code> of the <code>ContextMenu</code> is not changed/updated. Can someone help me get rid of this unexpected behavior and explain me the cause behind it?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDITS:</p>
  
  <p>I even marked the <code>NotifyIcon</code> object as a volatile object as I speculated that this was because of compiler caching. Turns out it wasn't as making the object <code>volatile</code> didn't solve the issue at hand.</p>