Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 30th, 2006, 04:29 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Windows Service Starts and Stops

Hi all,
I have created a windows service with .net 2.
I have installed the service but when I try to start it I get a message that the service has started and stopped as there was nothing to do.
I have noticed from some other similar problems on the net that this may be due to a thread not being started for each service.
There are three seperate services that I wish to run in this process and I have started them all from the program page like so:
static void Main()
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { 
                new SmsReceive(), 
                new SmsSend(),
                new SmsRouting()
In each of the services there is code like so:
        protected override void OnStart(string[] args)
            // TODO: Add code here to start your service.
            PortUtilities.OpenPort(serialPort, ReceiveGsmModemPort);
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
SO each has some kind of loop to continue watching either a GSM Modem or a Message Queue.

How can I get these services running in processes that will not stop?

Many thanks for your time in advance.

They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
Old May 13th, 2007, 05:16 PM
Authorized User
Join Date: Dec 2004
Location: Swindon, Wilts, United Kingdom.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz

Hi there,

With the Start() method, you cannot execute code the way you have displayed. The reason is that the method depends on the method finishing. That way, Windows knows that it has started sucessfully. What you have to do is create a new thread, and start the process running in the background, so that the start method can execute.

Here is a basic example. I won't put all the details in to save time.

public class MyService : ......
     private MyProgram runningProcess;
     private Thread runningThread;

     protected .... OnStart()
          runningProcess = new MyProgram();
          runningThread = new Thread( new ThreadStart( runningProcess.Run );

     protected ..... OnStop()

public class MyProgram
     public void Run()
          //Long running process code here

     public void Stop()
          //Code to stop the process
If you refer to the MSDN documentation on writing service, they should be able to give to a more detailed explanation.

Hope this helps


Similar Threads
Thread Thread Starter Forum Replies Last Post
Impersonate with windows service for Service A/C vinod_yadav1919 C# 0 October 18th, 2008 02:29 PM
Tomcat suddenly stops after starts annumol Apache Tomcat 2 February 9th, 2008 01:32 PM
Accessing Windows service from a windows app sajid08 C# 1 October 6th, 2006 10:25 AM
Windows service suddenly stops work. shai5 .NET Framework 1.x 0 August 14th, 2006 01:25 AM
Service Starts and Stops rodmcleay .NET Web Services 1 January 30th, 2006 04:30 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.