title: How to determine if a WCF service that is hosted in a Console application is up and running?
body: <p>I have a WCF Service that is hosted by a Console Application.
  The client connects to the Service through a named pipe.
  And the Console only gets executed when the client needs it, and the console gets killed after the client is done.</p>
  
  <p>Here is the code that starts and calls the service:</p>
  
  <pre><code>Process hostProcess = Process.Start(info);
  
  //make sure the service is up and running
  //todo: find out a better way to check if the service is up and running.
  Thread.Sleep(200);
  
  EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/test");
  NetNamedPipeBinding binding = new NetNamedPipeBinding();
  IHostedService service=hannelFactory&lt;IHostedService&gt;.CreateChannel(binding, endpointAddress);
  service.Run();
  
  hostProcess.Kill();
  </code></pre>
  
  <p>I am using Thread.Sleep to make sure the service is up and running, but that is definitely not the right approach to do so.</p>
  
  <p>So, how can I determine if a WCF service that is hosted in a Console application is up and running?</p>
  
  <p><em><strong>follow up question, how can i wait for the event to be fired without using Thread.Sleep?</em></strong></p>
  
  <pre><code>        private static EventWaitHandle GetEventWaitHandle()
      {
          try
          {
              EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(string.Format(serviceStartedEventName, taskIndex));
              return eventWaitHandle;
          }
          catch (Exception)
          {
              //if we do not sleep here, it may cause a stack over flow exceptoin.
              Thread.Sleep(10);
              return GetEventWaitHandle();
          }
      }
  </code></pre>