title: Finally is not executed when in a Thread running in a Windows Service
body: <p>Can anyone explain why this finally block is not executed? I have read posts about when to expect finally block not be executed, but this seems to be another case. This code needs TopShelf and log4net. I am running .net 4.5</p>
  
  <p>I guess it must be the Windows Service engine that kicks in on unhandled exceptions, but why is it running before the finally block has finished?</p>
  
  <pre><code>using log4net;
  using log4net.Config;
  using System;
  using System.Threading;
  using Topshelf;
  
  namespace ConsoleApplication1
  {
      public class HostMain
      {
          static void Main(string[] args)
          {
              HostFactory.Run(x =&gt;
              {
                  x.Service&lt;HostMain&gt;(s =&gt;
                  {
                      s.ConstructUsing(name =&gt; new HostMain());
                      s.WhenStarted(tc =&gt; tc.Start());
                      s.WhenStopped(tc =&gt; tc.Stop());
                  });
  
                  x.RunAsLocalSystem();
                  x.SetServiceName("TimerTest");
              });
          }
  
          public void Stop()
          {
              LogManager.GetLogger("MyLog").Info("stopping");
          }
  
          public void Start()
          {
              XmlConfigurator.Configure();
  
              LogManager.GetLogger("MyLog").Info("starting");
  
              new Thread(StartServiceCode).Start();
          }
  
          public void StartServiceCode()
          {
              try
              {
                  LogManager.GetLogger("MyLog").Info("throwing");
  
                  throw new ApplicationException();
              }
              finally
              {
                  LogManager.GetLogger("MyLog").Info("finally");
              }
          }
      }
  }
  </code></pre>
  
  <p>outputs</p>
  
  <pre><code>starting
  throwing
  stopping
  </code></pre>
  
  <p>EDIT: Please comment why you are downgrading, maybe you don't understand the problem? I see a big problem here. You write some domain logic that does important stuff in the finally clause on Exception. Then if you host the logic in a Windows Service the design suddenly is broken. </p>