I'm playing around with c# cos I was bored and I wanted to have a go at making a simple windows service that spits out a quote of the day if you telnet on a certain port.

I have it working but I'm not sure of the best way to stop the service. The OnStart method creates a thread and starts it. The method the thread executes is a permanent loop while(true){ create tcp listener etc..}

What is the cleanest way to cause the loop to break and the thread to return from the OnStop method of my service class?

Recommended Answers

All 5 Replies

Ok I added a private member variable:

Private bool RUNNING;

Set this to true in the OnStart method of the service class. Set it to false in the OnStop method.

Set the while loop to test the value of RUNNING:

	Socket s = this.l.AcceptSocket();
	Char[] carr = quotes[numServed%2].ToCharArray();
	Byte[] barr = Encoding.ASCII.GetBytes(carr);
	s.Send(barr, barr.Length, 0);

That left just one last problem, the loop is stuck waiting in the AcceptSocket method of the TcpListener, so even when the value of RUNNING is set false, it doesn't get tested until one further connection is made after the service is stopped.

I imagine the elegant way is to signal the waiting socket somehow, but the easy way is to have the OnStop method issue a connection of it's own to its own listening socket to trip the loop and have it evaluate false and exit the thread. Thus:

protected override void OnStop()
			// TODO: Add tear-down code here (if required) to stop your service.
			this.RUNNING = false;
			//Make a connection to force an iteration in method qotdSocketOpen()
			Socket socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			IPEndPoint remote = new IPEndPoint(ip, port);

Problem Solved !


You solved almost both of your questions. :) You may also use asynchronous socket server connection waiting using delegates.

Loren Soth


You solved almost both of your questions. :) You may also use asynchronous socket server connection waiting using delegates.

Loren Soth

Can you please teach me that?
I really need to have a windows service with an async socket in it.


You can always call Abort on the thread.


If you need to do any cleanup, put a try-catch around your while loop.

You can always use a polling methodology:

private void TcpListen()
                LogMan.Report("Starting Listener on port {0}.", SettingsManager.ListenerPort);

                TcpListener listener = new TcpListener(IPAddress.Any, SettingsManager.ListenerPort);

                while (true)
                    if (_stop)

                    if (!_pause)
                        if (listener.Pending())
                            TcpClient client = listener.AcceptTcpClient();
                            LogMan.Report("Client Connected to Listener.");
                            Thread thread = new Thread(new ParameterizedThreadStart(HandleClientConnection));
            catch (Exception ex)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.