views:

746

answers:

7

Hi, I'm writing a simple Windows Service that sends out emails to all employees every month. My question is, how to stop itself when it's done? I'm a noobie in this field so please help me out. Really appreciated.

It will be deployed on the server to be run monthly. I did not start this thing and the code was given to me like that. It is written in VB.NET and I'm asked now 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 sub is done, what it the status of this service? Is it stopped or just hung in there? Sorry, as I said, I am really new to this....

+5  A: 

If you have a task that recurs monthly you may be better off writing a console app, and then using Windows Task Scheduler to set it to run monthly. A service should be used for processes that need to run for a long time or constantly, with or without a user logged on

Macros
It will be deployed on the server to be run monthly. I did not start this thing and the code was given to me like that. It is written in VB.NET and I'm asked now 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 sub is done, what it the status of this service? Is it stopped or just hung in there? Sorry, as I said, I am really new to this....
cplusplusNewbie
If your new to this then this is a good time to make yourself look good and approach the decision-makers with Macros' suggestion. Services are not for once-a-month usage, they are continuous. For the case you mentioned, it's very simple to pull the code out of a windows service and into a console application.
MattH
+3  A: 

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

On the other hand, if you're just sending emails once a month, why are you using a service at all? Use the Windows Task Scheduler and run a normal application or script.

Roger Lipscombe
A: 

It might be better to write this as a scheduled task, it would certainly be easier to develop initially. Then it would naturally terminate and wouldn't be consuming resources for the rest of the month.

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

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

Then look for the one you want and set the status to stopped

locatedService.Status == ServiceControllerStatus.Stopped

Full example on msdn

Colin Gravill
A: 

net stop [service_name] ...on the command line will do it too.

But, I agree with everyone else; it seems that Windows Task Scheduler will meet your needs better.

Chris Dwyer
+2  A: 

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

However, if you are obligated for some reason to run as a Windows Service and you're working in .NET, you just have to call the Stop() method inherited from ServiceBase once your service completes its work. From the MSDN documentation for the method:

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

There's one important caveat here: the user account under which the service is running must have permission to stop services (which is a topic for ServerFault).

Once a service's OnStart method completes, it will continue running (doing nothing) until something tells it to stop in one of the following ways:

Jeff Sternal
A: 

Is there a reason it has to be a Windows service? If not, then follow @Macros solution. However, if it does, then why stop the service? If you stop it, then it'll just have to be restarted when the emails need to be sent. Based on your description, it doesn't sound like it would require a lot of resources, so I'd suggest just installing it and letting it run, firing up once a month to send the emails.

Matt Davis
A: 

here's what i did in a similar situation.

windows service runs 24/7 and processes work units. it gets work units through a database view.

table Message
 ProcessingStartTime
 CompletionDTE
 ...

the database view only pulls records marked not-complete and have a ProcessingStartTime in the past. So after the service confirms the transaction it executes a stored procedure that updates the database record. For this system, end-user upload excel files to asp.net webfrom that imports them into the database.

Chad