Running external process from windows service
I have a windows service that uses some third party application. That application is built from a single window for login, after the login the process is running in the background and not bother anyone.
I have code that opens this app开发者_JAVA技巧lication (using Process class), fills all the necessary fields and hit the connect button. This code works perfectly when I using it from a command line or win form, however executing this code from the windows service do nothing – not errors and no exe is running!
I know that this sounds a bit strange and windows service should be UI free by design but I need to lunch this app before my service can start to work and I don’t want to make this operation by two steps - lunching the external all and the starting the service.
I tried to check the "allow service to interact with desktop" from the logon tab in service properties but this did nothing.
Any suggestions how to make this work, or perhaps other design for this problem that get me to manage to do the operation in single step.
Thanks.
Maybe this might be able to help you? http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx
Note that it is for the Windows SDK though, and you would need to use PInvoke to execute the methods there.
Also see this related question: How to start a process from windows service into currently logged in user's session
You need to recalibrate your expectations. Launch your service automatically when the system starts. Launch your app when a user logs in (e.g. HKLM\Software\Windows\CurrentVersion\Run
). Arrange that the service is resilient to being started before the app. Also ensure that the service can survive the app stopping (e.g. crashing) and being restarted.
Why do I say all this? Well, services are intended to run at all times, even if there are no interactive users connected. If that is so then you simply cannot expect your interactive app to be running before the service starts.
I've had similar issues before with a windows service that needed to show a form on certain conditions, but most of the time just sit idle in the background.
Not sure what your requirements are, but I ended up writing it as a forms application with no main form.
Couple this with having the program launch at startup and it works pretty well.
Basically
[STAThread]
static void Main(string[] args)
{
// Launch your stuff in a separate thread like you would in the Service Start method.
Application.Run();
}
and that's it. Hope it helps.
精彩评论