title: Windows Service Exception Handling and Timers
body: <p>I've an issue with System.Timers.Timer. 
  Precisely it doesn't throw any Exceptions. Here is my code:</p>
  
  <pre><code>private Timer MyTimer { get; set; }
  
  public MyService()
  {
      InitializeComponent();
      AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
      MyTimer = new Timer(10 * 1000);
      MyTimer.Elapsed += MyTimer_Elapsed;
  }
  
  protected override void OnStart(string[] args)
  {
      MyTimer.Enabled = true;
  }
  
  private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  {
      if (!EventLog.SourceExists(EVENTLOGSOURCE))
      {
          EventLog.CreateEventSource(EVENTLOGSOURCE, EVENTLOGDESCRIPTION);
      }
      EventLog.WriteEntry(EVENTLOGSOURCE, "UnhandledException\r\n" + e.ExceptionObject, EventLogEntryType.Error);
      EventLog.WriteEntry(EVENTLOGSOURCE, "UnhandledExceptionEventArgs.IsTerminating: " + e.IsTerminating, EventLogEntryType.Error);
  
      if (e.IsTerminating)
      {
          this.ExitCode = 100;
          //this.Stop();
      }
  }
  
  private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
  {
      MyTimer.Enabled = false;
      CurrentDomain_UnhandledException(AppDomain.CurrentDomain, new UnhandledExceptionEventArgs(new Exception("FakeExceptionForTestPurposeOnly: It will be logged!"), false));
  
      Int32 i = Convert.ToInt32("10_ForSureGeneratesException_10");
  }
  </code></pre>
  
  <p>So, in my event log i can find "FakeExceptionForTestPurposeOnly: It will be logged!" but it's the only one! </p>
  
  <p>Afterall, the worse part is the service it's still running. <strong>How that even possible?</strong></p>
  
  <p><strong>Please note</strong> i already solved the problem, in that case the use of timer is not mandatory. System.Threading.Thread does the trick and works as a clockwork. I'm just curious i guess...</p>
  
  <p>So the question is: <strong>why during the occurrence of any UnhandledExceptions in MyTimer_Elapsed() "CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)" never will be fired?</strong></p>
  
  <p>Any helps, suggestions, comments will be appreciated</p>