I wrote a windows service with C#.Net. There is nothing in stop function. So, this means the service never stops and executes like daemon?

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

I wrote a Windows service with C#.Net. In its start function, it calls for a cmd prompt command. and in service code, there is nothing in stop function. So, this means the service never stops and executes like daemon? I mean is it working conitinuously? Do I need to write code to stop it in order to save resources?

Note that there is no problem with the service. It executes normally.

The aim of the question is understanding the service concept better.

Best regards.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The start and stop functions are event handlers.  The code in the stop function is only called when you stop the service using the service control panel or net command, ect.  You only need code there if there are things you need to clean up on stop; Files or DB connections to close, things like that.  Most services run continuously and are only stopped for special circumstances.  FYI, there are also Pause and Resume event handler methods that you can work with.  For instance if your service uses a timer to execute some functionality every x secs, you should disable the timer on  Pause and enable on Resume.
>>The code in the stop function is only called when you stop the service using the service control panel >>or net command, ect.

My service calls command prompt and do arp -s that sets the arp as static. I don't want anybody to release arp with arp -d. Normally, he has the priv. to do that. My aim is calling my arp -s continuously. So, since, there is no code in my stop and the service is started. Does that service run continuously?

I mean someone can arp -d and does my service change the settings instantly? (that's what I want.)

or does the service run once and finishes its job?

Best regards.
The user cannot enter the services since he is under domain. But how can he do with net command?
Can anybody reach services with some command through command prompt except for services.msc? (which he cannot since the action is restricted under domain)
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Commented:
1)  your service will only call arp -s continuously if you have some sort of endless loop or timer.   You should think of a service as just a normal program with 2 special features.  a) Instead of running using the permissions of the logged in user, it runs using the permissions of the account you assign to it.  b) if a user logs out it doesn't stop the program.  HOWEVER, if the program is written so that if it were a normal program and the user didn't log out it would just run once and then stop, then that is what your service will do.  You need to code the OnStart to match what you would write in the Main method of a regular exe to keep it running over and over while the user is logged in.

2)net command also does security checking.  If you can't stop in services.msc you can't using net command.
ok...I got the point. So, there are 200 machines actively running. If I do my stuff with infinite loop, what should be the performance of the network connections or performance machines having p4, 1 G RAM each?

Commented:
I am not an ARP expert but I believe that arp -s updates the local cache only, it doesn't generate any network traffic itself.  What the load on the idividual boxes would be I couldn't say you would need to try it.  You might want to use a timer instead of an infinite loop.  With a timer you could limit the calls to once every second or so, as opposed to as fast as the loop can execute.
Which one is better? Note that the application has no gui.

Using sleep method, in the start function of the service:

using System.Threading;
Thread.Sleep(50);

or

using Timer (that I haven't used it before):
Is above logic true?

//in start() function of the service:

while(true){
Timer timer1 = new Timer(500);
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);
}

//and in class
public static void OnTimerEvent(object source, EventArgs e)
{
//doing the cmd job
}

Best regards.
Commented:
Sleep might be a little better in terms of overhead but both are fine.  However if you use the timer you only need to set up the timer once.  The purpose of the timer is to fire the Tick event once every x millsec as long as the timer is enabled.  In a sense the timer has a built in while loop.  Also you need to declare the timer as a class level variable.  A the top of the class code something like:
private Timer timer1;

If you declare the timer in the start method once the method is done the timer would go out of scope and when garbage collection happend the timer would quit working.

body of start is either
while(true)
{
   Thread.Sleep(500);
   //doing the cmd job
}

or

timer1 = new Timer(500);
timer1.Enabled = true;
timer1.Tick += new System.EventHandler (OnTimerEvent);

Commented:
The other benefit of the Timer is that you can fully implement you service by handling the Pause and Resume events.  Don't have to, but it is good practice.

OnPause method would be timer1.enabled = false;
OnResume method would be timer1.enabled = true;
>>However if you use the timer you only need to set up the timer once.
meaning, have the following before while;
Timer timer1 = new Timer(500);
timer1.Enabled = true;

>>The purpose of the timer is to fire the Tick event once every x millsec as long as the timer is enabled.
meaning, timer1.Tick += new System.EventHandler (OnTimerEvent); in your while loop.

>>Also you need to declare the timer as a class level variable.
yep, i miss that.

so the code for timer is as:
private Timer timer1;

body of start():
timer1 = new Timer(500);
timer1.Enabled = true;

while(true)
timer1.Tick += new System.EventHandler (OnTimerEvent);

body of eventhandler():

public static void OnTimerEvent(object source, EventArgs e)
{
//doing the cmd job
}

Sorry, in this machine, I have no compiler, that is the reason why I am writing the code I should write prior compiling.

>>OnPause method would be timer1.enabled = false;
>>OnResume method would be timer1.enabled = true;
thank you:)

Thanks for your interest.

Best regards.

Commented:
If you use a timer you don't need a while loop at all.  The timer loops internally for you firing off the Tick event every (in this case) 500 ms.  The timer1.Tick += line tells your code that each time the Tick event occurs run OnTimerEvent method.  You only need to "wire this up" once.    I would also rearrange the code to:

timer1 = new Timer(500);
timer1.Tick += new System.EventHandler (OnTimerEvent);
timer1.Enabled = true;
got your point.
For the info:
timer1.Tick += new System.EventHandler (OnTimerEvent);
gives error on Tick even I import the references.

Note that this is a service not a windows form application. I mean no drag and drop stg is possible since it's a service. So, I did the thread.sleep stuff.

Best regards.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial