title: Windows Service thread is not stopping in timely fashion in c#
body: <p>I have a windows service which is getting started using Thread.After installation as windows service i am able to start the service properly but as soon as i try to stop the service it is taking too much time and and not getting stopped.I am using <code>ManualResetEvent</code> to stop the windows service Here is my code.</p>
  
  <pre><code>protected override void OnStart(string[] args)
      {
          _thread = new Thread(DoWork);
          _thread.Start();
  
      }
      private void DoWork()
      {
  
              while (!_shutdownEvent.WaitOne(0))
              {
                  data = new byte[1024];
                  int recv = sock.Receive(data);
                  stringData = Encoding.ASCII.GetString(data, 0, recv);
              }
              sock.Shutdown(SocketShutdown.Both);
              sock.Close();
          }
          catch (Exception DFGFD)
          {
          }
  
      }
  
      protected override void OnStop()
      {
          _shutdownEvent.Set();
          _thread.Join();  // wait for thread to stop
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me to resolve this.</p>