2

I have a startup application that relies on a service (which also runs at startup). Will services always run first?

My goal is to know whether my application can assume that if the service is not running - it hasn't been installed (or some error has occurred) and the user should be notified or the service should be installed. If this is not necessarily true (that services run first), I would have the app wait some time and recheck before alerting the user or trying to install the service. (10 seconds? 2 minutes?)

EDIT

I am aware that a service might be intentionally not executed immediately (Delayed Start). I'm not referring to that.

More info: The service is mine as well. Installed by the app on first run asking the user for admin privileges. And the app is a UI app so can't run as a Service.

ispiro
  • 23,513
  • 30
  • 116
  • 236
  • there is the startup option "delayed start" for services. I'm not sure about other services, but at least for delayed start it is empirically true that they can launch after user and desktop initialization is done. [related](https://stackoverflow.com/a/11015576/1132334) – Cee McSharpface Apr 27 '18 at 14:41
  • @dlatikay Thanks. I'm asking about a specific service that is not with delayed start. – ispiro Apr 27 '18 at 14:42
  • 1
    Would it be an option to have your application check to see if the service is already running, and if not, start it? – Richard Ev Apr 27 '18 at 14:47
  • @RichardEverett Sort of. The service might not have even be installed, and I don't think it would be wise to try to "install" it while it's trying to start. – ispiro Apr 27 '18 at 14:49
  • if the service is a component of an application you ship, you should use the SCM API to manage installation and query the services' status. note that "starting" is also a status, so you can well distinguish "continue waiting" from "send start command". – Cee McSharpface Apr 27 '18 at 14:50
  • ServiceController() will tell you if its running (or not installed), if its not you wait for an appropriate period of time then retry. – Alex K. Apr 27 '18 at 14:51
  • You could run a search on the system, but it would need to have been registered and accessible in regedit, and then run a check to see if its installed or not, and if not flag to the user to do the install. If its there and not running start it up – Simon Price Apr 27 '18 at 14:52
  • It sounds like a tricky situation. Hard to know what to recommend without knowing a bit more about the scenario. Could your application itself be reworked to be a Windows Service, configured to have a dependency on this other service? – Richard Ev Apr 27 '18 at 14:54
  • @dlatikay Checking if it's starting sounds good. Do you know if it's in that status even before it's really starting (and mean something like "we're working on it") or would it only be "starting" when it's actually in the middle of starting, therefore not completely solving the issue? – ispiro Apr 27 '18 at 14:54
  • @RichardEverett No. My application is a UI application (systray). – ispiro Apr 27 '18 at 14:55

1 Answers1

1

Will services always run first

Nope. definitely not. The service might took too long to start, or its dependency failed to start and it has to wait for that dependency to retry ect.

I have experienced this myself with the windows time service

However you have couple options for this

  1. Use task scheduler and set the trigger condition to "On an event" and figure out what Eventlog entry the particular service would generate on start.

  2. Make your application a service and declare that its depended on the service it requires. You will get an error stating the dependency is not met if the service is not installed.

  3. Allow the application to startup but checks every X seconds to make sure the service is up before continue.

Steve
  • 10,544
  • 6
  • 29
  • 66