title: C# windows service within console application code after ServiceBase.Run not executed?
body: <p>I implemented windows service with in console application as follows:</p>
  
  <pre><code>  internal class Program
  {
      private static void Main(string[] args)
      {
  
              ServiceBase.Run(new MyServicesInitializer()); //host the services in managed windows service
  
           //some more code
           string x=1;
           .....
  
      }
  
  
  
   public class MyServicesInitializer : ServiceBase
   {
         protected override void OnStart(string[] args)
         {
                 //my code
         }
   }
  </code></pre>
  
  <p>My question is: when I start the service with sc.exe do the main method is called? it seems like not... If someone can explain the flow what is happening when I start the service with sc and for what reason I need the line: <strong>ServiceBase.Run(new MyServicesInitializer());</strong>  in my main?</p>
  
  <p>Edit: I did experiment and throw exception before and after the line in the main: when I throw exception before the exception was thrown <strong>but</strong> when I put the exception after the run method it is not thrown and the service started successfully... Someone can explain why the code after the Run method is not executed?</p>