| | |
Windows service and threads and sockets
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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?
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?
Ok I added a private member variable:
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:
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:
Problem Solved !
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
while(RUNNING){ Socket s = this.l.AcceptSocket(); Char[] carr = quotes[numServed%2].ToCharArray(); Byte[] barr = Encoding.ASCII.GetBytes(carr); s.Send(barr, barr.Length, 0); s.Shutdown(SocketShutdown.Both); s.Close(); numServed++; }
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:
C# Syntax (Toggle Plain Text)
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); socClient.Connect(remote); socClient.Shutdown(SocketShutdown.Both); socClient.Close(); }
Problem Solved !
•
•
Join Date: Mar 2008
Posts: 23
Reputation:
Solved Threads: 5
You can always call Abort on the thread.
If you need to do any cleanup, put a try-catch around your while loop.
C# Syntax (Toggle Plain Text)
m_thread.Abort();
If you need to do any cleanup, put a try-catch around your while loop.
![]() |
Similar Threads
- How to create a windows service? (Visual Basic 4 / 5 / 6)
- How to add any task to windows service? (C#)
- Windows Service Problem (C#)
- Problem with Windows Service (C#)
- Windows XP Service Pack 2 (Windows NT / 2000 / XP)
- windows service pack 2 installation - unique problem (Windows NT / 2000 / XP)
- Service Pack 2 Update Windows XP Pro (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: Custom Templates (Generics)
- Next Thread: How to choose printer with StartInfo.Arguments
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation visualstudio webbrowser windows winforms wpf xml







You may also use asynchronous socket server connection waiting using delegates.