CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    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?

  2. #2
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    171

    Re: Stopping a service from inside

    I am looking for the answer to this question also... Anyone have any ideas???

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  4. #4
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    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.

  5. #5
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    171

    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?

  6. #6
    Join Date
    Jan 2005
    Location
    Brighton, England
    Posts
    241

    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.

  7. #7
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    171

    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.

  8. #8
    Join Date
    Mar 2005
    Location
    Chicago, IL
    Posts
    69

    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 .

  9. #9
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    171

    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.

  10. #10
    Join Date
    Mar 2005
    Location
    Chicago, IL
    Posts
    69

    Re: Stopping a service from inside

    You are correct Noods .

    stop the timer and then do this

    Code:
    ServiceController controller = new ServiceController();
    controller.MachineName = ".";
    controller.ServiceName = "STOP SERVICE";
     
    // Stop the service
    controller.Stop();


    You can stop or start the service from the service controller
    Last edited by sudheeratwork; June 29th, 2005 at 12:18 PM.
    There has been an alarming increase in the number of things I know nothing about .

  11. #11
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    171

    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
  •  





Click Here to Expand Forum to Full Width

Featured