Service fails if user not logged in

J

JamesB

A week or two ago I posted queries about retrieving user details about a
certain process and it all worked fine in the end, the service happily
reported when the required application was started/stopped and everyone was
happy.
A new problem has come to light though, that when someone turns on their PC,
my service isn't starting. Looking in the event log, I have the following
error:

Service cannot be started. System.Runtime.InteropServices.COMException
(0x80010002): Call was canceled by the message filter. (Exception from
HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))
at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32
errorCode, IntPtr errorInfo)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementEventWatcher.Initialize()
at System.Management.ManagementEventWatcher.Start()
at MyService.ProcessInfo..ctor(String appName)
at MyService.NetviewerMonitor.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

The code I have in my OnStart event is:

//Process object
private ProcessInfo nvProc;

protected override void OnStart(string[] args)
{
nvProc = new ProcessInfo("App_to_watch.exe");
nvProc.Started += new
ProcessInfo.StartedEventHandler(nvStarted);
nvProc.Terminated += new
ProcessInfo.TerminatedEventHandler(nvTerminated);
}


I am guessing the error is occurring because when the service starts no one
is logged in? This makes sense, because if a user starts the service
manually once they log in, it works perfectly. If so, any ideas what I can
do? I don't think sticking a try/catch block would help - it would stop the
error in event log, and the service would still run, but I doubt it would
work as the code to attach to the new process event wouldn't have fired, so
it would be chocolate teapot time... Can I put in some sort of block in my
OnStart to wait until a user is logged in? Seems a bit dodgy, as presumably
my service wouldn't show as started until the OnStart() completes and
Windows woud probably log errors to the effect of the service didn't start
within the timeout period...

Thanks.
James
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Have you tried to use SystemAccount to run the process under?
 
J

JamesB

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Have you tried to use SystemAccount to run the process under?

Under the "LocalSystem" account, the code that retrieves the user of the
process I am watching always returns null, so it solves one problem but
creates another!
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

JamesB said:
Under the "LocalSystem" account, the code that retrieves the user of the
process I am watching always returns null, so it solves one problem but
creates another!

The thing is that a Service is intended to be used as a background app. It
does start when the system start and at that moment there is no user logged
in.

You could try to use WMI and listen to events that might be fired when a
user login. But I do not know what events those are :)
 
J

JamesB

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,



The thing is that a Service is intended to be used as a background app. It
does start when the system start and at that moment there is no user
logged in.

You could try to use WMI and listen to events that might be fired when a
user login. But I do not know what events those are :)

That's kinda where I got to, might have to do a rethink and actually deploy
it as a non-service app that simply starts when a user logs in (via a
reg-entry etc)

Thanks
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top