views:

29

answers:

3

I'm developing a Windows service which will run at a specific time every day. What will happen if multiple users are logged in to that machine? Will it run 5 times at the same time if there're 5 users logged in? If it does, is there a way to prevent it?

+1  A: 

Windows services run once per machine.

The account under which the service runs can be specified in the windows Services MMC console. Generally, a service will run under the "Local System" account.

If you need your app to run only once per computer, you are headed in the right direction with a service. However, if you need it to run on a schedule, a simple app that is triggered by Windows Task Scheduler may be a better route.

John Gietzen
+5  A: 

A windows service is installed machine-wide and runs under one specified user account, which may in fact not be a "user," but "Local System" or "Network Service" or similar.

The service will run only once without regard to logged on users. In fact, it will run if the computer is on, even if no user has logged on.

Jay
It will run if it is configured to run on startup...
Oded
@Oded Good point -- this corresponds to "Automatic" start (as opposed to "Manual" or "Disabled").
Jay
@Jay - Manual also applies if there is an "Automatic" service that depends on it.
Oded
+1  A: 

As others have answered, no, it will only run once.

This does raise some flags for me though. Why are you writing a Windows Service for this? Seems like overkill for a process that only needs to run once. Services are generally used for items that constantly need to perform a task, monitor something, or receive events. I dont know you specific goals, but perhaps a scheduled task may be better fit. Downsides are that a scheduled task will only run when a user is logged in, and they may be specific for a user.

If this is in a enterprise or corporate environment, perhaps there is a operations team that has scheduling software, and you could set this up as some kind of batch job.

Mike Ohlsen
I don't agree that services are for constant tasks. A great many services just run a timer for hours, days, or even weeks and then do their work. Consider the many "software update" services that pop up occasionally.
Jay
good point Jay.
Mike Ohlsen