Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 30th, 2006, 03: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:
Code:
static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { 
                new SmsReceive(), 
                new SmsSend(),
                new SmsRouting()
            };
            ServiceBase.Run(ServicesToRun);
        }
In each of the services there is code like so:
Code:
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            PortUtilities.OpenPort(serialPort, ReceiveGsmModemPort);
            serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            Watch();            
        }
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.
Rod



======================================
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."
--Shakespeare
======================================
Reply With Quote
  #2 (permalink)  
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
Default

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.

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

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

     protected ..... OnStop()
     {
          runningThread.Stop();
     }
}

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

Dominic
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 12: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 03:30 AM



All times are GMT -4. The time now is 04:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.