-
January 5th, 2005, 10:30 AM
#1
Stopping a service from inside
Hi,
how can I stop a service from inside of the service? What I want is to stop the service if an error is occurred during the execution of the OnStart() method. Here my method:
Code:
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
try
{
//full name of the configuration file
configFilename = args[0].Trim(new char[2] {'/',' '});
//get properties from configuration file
RemotingConfiguration.Configure(configFilename);
}
catch(IndexOutOfRangeException)
{
this.EventLogger.WriteEntry("Configuration file missed.", EventLogEntryType.Error);
}
catch(ArgumentException)
{
this.EventLogger.WriteEntry("Configuration file could not be loaded.", EventLogEntryType.Error);
}
}
What I want is to stop the service after catching the errors. How I can do that?
-
June 28th, 2005, 05:57 PM
#2
Re: Stopping a service from inside
I am looking for the answer to this question also... Anyone have any ideas???
-
June 29th, 2005, 01:20 AM
#3
Re: Stopping a service from inside
You need to have a thread which does a job of the service. This thread is typicaly started in OnStart() method. If you don't start this thread, the service process will quit immediately. I think that RemotingConfiguration.Configure(configFilename) is similar to that thread, so simply don't call it. In other words - if everything goes right, the service will work, if something goes wrong, the service want start. You can also (re)throw an exception, it also should prevent the service from starting.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
June 29th, 2005, 05:24 AM
#4
Re: Stopping a service from inside
are you using service controller to start and stop the service?
If you think you CAN, you can, If you think you CAN'T, you are probably right.
Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.
-
June 29th, 2005, 10:09 AM
#5
Re: Stopping a service from inside
I can start the service no problem, and I can stop it no problem from the service controller in windows. What I need is that once the service has started and been running it may encounter a situation where it needs to shut itself down.
How do I go about that?
-
June 29th, 2005, 10:17 AM
#6
Re: Stopping a service from inside
SImply return from your Service's main thread and that should cause it to exit.
You should let the operating system know your service is shutting down though, by setting the appropriate SERVICE_STOP_PENDING state.
-
June 29th, 2005, 10:35 AM
#7
Re: Stopping a service from inside
I'm not exactly sure how to do that, as all I do in the OnStart() of the thread is create an object. That object has a timer in it and when the timer fires i do some processing. Now my object may decide that it is time to shut down.
I don't need people to code it for me, but if anyone knows of an example somewhere, that would be great as I've searched and searched and this was the only thread anywhere that I've found.
-
June 29th, 2005, 10:51 AM
#8
Re: Stopping a service from inside
When an error has occured do something like this
Code:
timer1.Enabled = false;
and the service stops.....
There has been an alarming increase in the number of things I know nothing about .
-
June 29th, 2005, 11:00 AM
#9
Re: Stopping a service from inside
No, the service will not stop that way. The service will stop processing, which is good, but the service executable will still show up in task manager and the service controller will still show the service as running, which is bad.
-
June 29th, 2005, 12:01 PM
#10
-
June 29th, 2005, 12:22 PM
#11
Re: Stopping a service from inside
lol ... thanks, I just figured that out on my own and was coming here to post and let everyone else know who might need this information as well.
Thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|