title: Why does invoking an event in a timer callback cause following code to be ignored?
body: <p>I'm writing a simple game that uses timers from the <code>system.threading</code> namespace to simulate wait times for actions. My goal is to have the timer execute once every second for x amount of seconds. To achieve this I added a counter in the timer callback.</p>
  
  <p>The problem is any code I place after invoking the <code>DeliveryProgressChangedEvent</code> event seems to get ignored. My counter is never incremented thus allowing the timer to run forever.</p>
  
  <p>If I invoke the event after I increment the counter, everything works fine. Just nothing after invoking the event will execute. Rather than going the easy route I'd like to understand if not resolve this problem.</p>
  
  <p>I did a fair bit of research into the system.threading timer object as well as events but wasn't able to find any information related to my issue. </p>
  
  <p>I created a bare bones example of my project to demonstrate the issue below.</p>
  
  <h1>Game Class</h1>
  
  <pre><code>    class Game
      {
          private Timer _deliveryTimer;
          private int _counter = 0;
  
          public event EventHandler DeliveryProgressChangedEvent;
          public event EventHandler DeliveryCompletedEvent;
  
          public Game()
          {
              _deliveryTimer = new Timer(MakeDelivery);
          }
  
          public void StartDelivery()
          {
              _deliveryTimer.Change(0, 1000);
          }
  
          private void MakeDelivery(object state)
          {
              if (_counter == 5)
              {
                  _deliveryTimer.Change(0, Timeout.Infinite);
                  DeliveryCompletedEvent?.Invoke(this, EventArgs.Empty);
              }
  
              DeliveryProgressChangedEvent?.Invoke(this, EventArgs.Empty);
  
              ++_counter;
          }
      }
  </code></pre>
  
  <h1>Form Class</h1>
  
  <pre><code>    public partial class Form1 : Form
      {
          Game _game = new Game();
  
          public Form1()
          {
              InitializeComponent();
  
              _game.DeliveryProgressChangedEvent += onDeliveryProgressChanged;
              _game.DeliveryCompletedEvent += onDeliveryCompleted;
  
              pbDelivery.Maximum = 5;
          }
  
          private void onDeliveryProgressChanged(object sender, EventArgs e)
          {
              if (InvokeRequired)
                  pbDelivery.BeginInvoke((MethodInvoker)delegate { pbDelivery.Increment(1); });
  
              MessageBox.Show("Delivery Inprogress");
          }
  
          private void onDeliveryCompleted(object sender, EventArgs e)
          {
              MessageBox.Show("Delivery Completed");
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              _game.StartDelivery();
          }
      }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Just to clarify what I mean. Any code I put after <code>DeliveryProgressChangedEvent?.Invoke(this, EventArgs.Empty);</code> will not execute. In my example <code>++_counter</code> will not run. The event does fire and the <code>onDeliveryProgressChanged</code> handler does run.</p>