title: How do I repeatedly run code in a C# system tray application (Like on a timer)?
body: <p>I started with a default Windows Forms Application in C# and all I changed was Progam.cs. From the main function, I changed</p>
  
  <pre><code>Application.Run(new Form1());
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Application.Run(new MyCustomApplicationContext());
  </code></pre>
  
  <p>Which refers to a custom class (MyCustomApplicationContext:ApplicationContext) that runs my program as a system tray icon instead of a Windows Form. The constructor contains this code:</p>
  
  <pre><code>private NotifyIcon trayIcon = new NotifyIcon();
  trayIcon.ContextMenu = new ContextMenu(
      new MenuItem[] 
      {
          new MenuItem("Exit", Exit)
      });
  </code></pre>
  
  <p>This allows the user to right click the icon, giving them a context menu with an "Exit" option that will run a function that closes out the program.</p>
  
  <p>At the end of the constructor of MyCustomApplicationContext, I call a recursive function in the class called Update() which executes a ping function and changes the system tray icon depending on the ping delay.</p>
  
  <p>Unfortunately I believe because it's recursive, it isn't allowed to run any other code, so the right click context menu doesn't ever show up. I'd rather call the Update() function through an event, like the System.Timers.Timer Elapsed event. I just don't know how events work or where to put the code.</p>