Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

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?

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.
Avatar of jamesrh
jamesrh
Flag of United States of America image

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.
Avatar of jazzIIIlove

ASKER

>>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)
ASKER CERTIFIED SOLUTION
Avatar of jamesrh
jamesrh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.