title: C# - Create setInterval / clearInterval functions
body: <p>I am new to C# and try to create a <code>setInterval</code> and <code>clearInterval</code> function which is exactly working like the same functions in javascript.</p>
  
  <p>I do it mainly for practice and to learn what can be done in C# and what not.</p>
  
  <p><strong>setInterval</strong></p>
  
  <hr>
  
  <p><strong>Requirements:</strong> Create a new timer and return it, also run a anonymous function or a predefined function again and again in the given interval.</p>
  
  <pre><code>    Timer setInterval(Func&lt;int&gt; myMethod, int intervalInMs)
      {
          var timer = new Timer();
          timer.Start();
  
          while (true) {   //probably a infinite loop
              if (timer.ElapsedMilliseconds &gt;= intervalInMs)
              {
  
                  myMethod();
                  timer.Restart();
              }
          }
  
          return timer;  //Code does never reach this part obviously because of the while loop
      }
  </code></pre>
  
  <p><strong>clearInterval</strong></p>
  
  <hr>
  
  <p><strong>Requirements:</strong> Stop the timer.</p>
  
  <pre><code>    void clearInterval(Timer timer)
      {
          timer.Stop();
      }
  </code></pre>
  
  <hr>
  
  <p><strong>Planned Usage</strong></p>
  
  <pre><code>    Timer myTimer = setInterval(delegate {
          MessageBox.Show(
              "test",
              "test",
              MessageBoxButtons.OK,
              MessageBoxIcon.Warning
          );
  
          return 1;
      }, 5000);
  
  
      //somewhere in code...
  
      clearInterval(myTimer);
  </code></pre>
  
  <p>How is it possible to solve this with C#, by using Events?</p>