How to programmatically stop a Windows service?

I write a simple Windows service that sends emails to all employees every month. My question is: how to stop yourself when this is done? I am a noobie in this area, so please help me. Really appreciated.

It will be deployed to the server to run monthly. I did not start this thing, and I liked the code so much. It is written in VB.NET and I will be asked to change a few things around it. I noticed that there is only โ€œSub OnStartโ€ and wondered when the service would stop? After the main south is done, what is the status of this service? Was he stopped or just hung there? Sorry, as I said, I'm really new to this ....

+6
windows-services
source share
7 answers

If you have a task that repeats monthly, you might be better off writing a console application and then using the Windows Task Scheduler to install it monthly. The service should be used for processes that need to be run for a long time or continuously, with or without user login

+7
source share

As noted in every other answer, it looks like it should be an executable or script that you run as a scheduled task.

However, if for some reason you are required to work as a Windows service and work in .NET, you just need to call the Stop() method inherited from ServiceBase as soon as your service finishes its work. From the MSDN documentation for the method:

The Stop method sets the state of the service to indicate that a stop is pending and calls the OnStop method. After the application is stopped, the service state is stopped. If the application is a hosted service, the application domain is unloaded.

There is one important caveat: the user account under which the service runs must have permission to stop the services (this is the topic for ServerFault ).

As soon as the OnStart service method is completed, it will continue to work (do nothing) until something tells it to stop in one of the following ways:

  • Programmatically, calling Stop inside the service itself or from an external process using the method Colin Gravil describes in his answer .
  • Through the command line.
  • From the Management console in the Windows Management console.
+6
source share

If it is a Win32 service (i.e. written in C or C ++), you simply call SetServiceStatus (SERVICE_STOPPED) and return from ServiceMain.

On the other hand, if you just send emails once a month, why do you use the service at all? Use the Windows Task Scheduler and run a regular application or script.

+3
source share

Perhaps it would be better to write this as a planned task, initially it would be easier to develop. Then it will naturally stop and will not consume resources for the remainder of the month.

To answer the original question, you can get a list of currently running services in C #

 services = System.ServiceProcess.ServiceController.GetServices(); 

Then find the one you want and set the status to a stop

 locatedService.Status == ServiceControllerStatus.Stopped 

Full example in msdn

0
source share

net stop [server_name] ... on the command line, this will do too.

But I agree with everyone; Windows Task Scheduler seems to better suit your needs.

0
source share

Is there a reason why was there a windows service? If not, then follow @Macros' solution. However, if so, why stop the service? If you stop it, you just need to restart it when you need to send emails. Based on your description, it doesnโ€™t sound like it would take a lot of resources, so I would suggest just installing it and letting it work once a month to send emails.

0
source share

this is what i did in a similar situation.

Windows 24/7 is running and processing units. it receives work units through a database view.

Table Message
ProcessingStartTime
CompletionDTE
...

The database view only pulls records marked as incomplete, and in the past has ProcessingStartTime. Therefore, after the service confirms the transaction, it executes a stored procedure that updates the database record. For this system, the end user uploads excel files to asp.net webfrom, which imports them into the database.

0
source share

All Articles