>> 1. System.Environment.UserInteractive gets you part way down the road -
>> from
[quoted text clipped - 40 lines]
> the service is just getting started at the same time that I launch the exe
> interactively, both processes will be told they are the service.
> How about creating a Mutex when the app is running as a Service? When
> your exe is run from the command line, it tries to create the same
> Mutex. If it can't, the service is installed already and running.
> This tells you that this instance is not the service instance.
Catch-22: when the service starts, how does it know it IS the service
instance? and not just the app being started when the service state is
currently stopped?
There are only a few things to distinguish between the service starting up,
and the program starting when the service is stopped. One is the parent
process: if started by the service control manger, I'm the service,
otherwise not. Related to that is that we might be able to match the
process ID with WMI; I'm still checking on that one. Another is the
username, but I'd rather not depend on the username because the enduser
might have changed it.
Ross Presser - 09 Jul 2005 00:23 GMT
Well, the results are in. Only tested on one Windows XP Pro machine so
far, but here's what I've got:
System.Environment.UserInteractive:
when started from commandline or shell: True
when starting service: False (yay!)
Service status:
When started from commandline or shell: depends on whether service is
running already
when starting service: 2 (StartPending) (yay!)
Matching my PID with the pid reported by WMI for the service:
When started from commandline or shell: empty collection
When starting service: empty collection (boo!)
Getting the parent process's name:
When started from commandline: cmd.exe
When started from shell: explorer.exe
When started from development environment: devenv.exe
When starting service: services.exe (yaay!!)
So it looks like I have a few winning methods. I think I will combine
them and only continue as a service if ALL agree.
Look for a writeup on my blog in the near future: rpresser.blogspot.com
Michael C# - 09 Jul 2005 04:41 GMT
>> How about creating a Mutex when the app is running as a Service? When
>> your exe is run from the command line, it tries to create the same
[quoted text clipped - 4 lines]
> instance? and not just the app being started when the service state is
> currently stopped?
Like I said, the Mutex method will only tell you that the instance you are
trying to start is not the Service instance if the service instance is
installed and running.
> There are only a few things to distinguish between the service starting
> up,
[quoted text clipped - 4 lines]
> username, but I'd rather not depend on the username because the enduser
> might have changed it.
All valid points, but then again why not just simplify your life and use the
command-line parameters, as you originally said? If it's started with no
command line parameters, then it's the service instance. To start the
interface portion make the user add /i (or some such) to the command line
for interface. Usually I just keep the interface in a completely separate
exe from the service, but whatever solution you come up with I'd definitely
be interested in seeing your results - I might start doing the single exe
thing as well.
Michael C# - 09 Jul 2005 04:41 GMT
>> How about creating a Mutex when the app is running as a Service? When
>> your exe is run from the command line, it tries to create the same
[quoted text clipped - 4 lines]
> instance? and not just the app being started when the service state is
> currently stopped?
Like I said, the Mutex method will only tell you that the instance you are
trying to start is not the Service instance if the service instance is
installed and running.
> There are only a few things to distinguish between the service starting
> up,
[quoted text clipped - 4 lines]
> username, but I'd rather not depend on the username because the enduser
> might have changed it.
All valid points, but then again why not just simplify your life and use the
command-line parameters, as you originally said? If it's started with no
command line parameters, then it's the service instance. To start the
interface portion make the user add /i (or some such) to the command line
for interface. Usually I just keep the interface in a completely separate
exe from the service, but whatever solution you come up with I'd definitely
be interested in seeing your results - I might start doing the single exe
thing as well.