2

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 application (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.

  • DO you see any errors, e.g. in Event Log or does your service log any error messages?? – marc_s Jul 01 '11 at 19:39
  • 1
    Not sure if this will help, but you could try running the service under an actual logon/user account, instead of SYSTEM or NETWORK SERVICE – Jay Jul 01 '11 at 19:40
  • How does your process 'hit the button'? You need to show some code. Have you tried debugging it step by step to see where exactly the result doesn't match your expectations? – Dan Abramov Jul 01 '11 at 19:41
  • I've merged your unregistered accounts. You should be able to use the site [normally](http://stackoverflow.com/faq) again. Hint: Don't post an answer that isn't a direct solution to your question. – Tim Post Jul 02 '11 at 18:26

3 Answers3

2

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

Community
  • 1
  • 1
Can Gencer
  • 8,822
  • 5
  • 33
  • 52
  • The OP actually asked to arrange that the interactive app started before the service. I think that's a non-starter, but you answer addresses a different problem, namely starting an interactive app from a service. – David Heffernan Jul 01 '11 at 19:47
  • 1
    I don't think that's what he meant. he means the app should start before the service starts to work, i.e. when you start the service it can start the app, and then do the actual work necessary after that. – Can Gencer Jul 01 '11 at 19:48
  • That could be it but if so why go to the trouble of getting the service to start the app? Just start it with `HKLM\Software\Windows\CurrentVersion\Run` or similar. – David Heffernan Jul 01 '11 at 21:02
1

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

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.

IronicMuffin
  • 4,182
  • 12
  • 47
  • 90