1

I have a service which should open an exe application ( C# Application ) based on certain conditions. When the service is started in debugging mode ( Visual studio ) it opens the application. But when it is installed as a service, it does not do so. It fails to open the application. Why is this happening?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Pavan Kumar
  • 157
  • 2
  • 4
  • 17
  • Does the user have to see the application? That is not possible. And does your service run in a session? That may be required. – TomTom Dec 01 '14 at 16:28
  • Services are by default run in an isolated session that does not allow interaction from the user. Windows Services are not intended to be interactive, so what you are trying to do here is not what a service is intended to do. You may want to consider refactoring your code as a user mode application. Clarify what you need the exe application to do, and what the service does? – Kenneth Aalberg Dec 01 '14 at 16:35
  • Yes, users have to see the application. No, it does not. any work around for this? – Pavan Kumar Dec 01 '14 at 16:35
  • Hi Kenneth, The application is a weekly attendance system(multiple users).It needs to be launched every monday and the user can postpone this operation by selecting the remind me option in the exe application. The time is recorded in a text file. The service here reads the text file and compares with the system date, if greater should launch the application.It also writes the date of next monday. It works fine in debugging mode, when installed, it fails. – Pavan Kumar Dec 01 '14 at 16:39
  • Am I understanding the question wrong, or do you just need to use `Process.Start`? That will even launch GUI apps just fine... – BradleyDotNET Dec 01 '14 at 16:43
  • Hi Bradley, Im currently using Process.Start . It does not launch the application. – Pavan Kumar Dec 01 '14 at 16:46
  • So just to be clear, you are just trying to start some external process from your service using `Process.Start`, and you can verify that the condition is met in the debugger (try attach to process, to debug the service mode). – BradleyDotNET Dec 01 '14 at 16:48
  • Yes, i did that, it satisfies the condition, enters the loop and executes Process.start, but the application wont launch. – Pavan Kumar Dec 01 '14 at 16:51
  • @BradleyDotNET - "fails to open the application" should mean "I can't see UI even if application starts" - note that OP tries to launch application from service (non-interactive session) AND see UI in interactive desktop session. As olitee pointed out it is not really possible and discussed to many times like http://stackoverflow.com/questions/5063731/is-there-any-way-to-start-a-gui-application-from-a-windows-service-on-windows-7, http://stackoverflow.com/questions/11548758/windows-service-launching-and-exe ... – Alexei Levenkov Dec 01 '14 at 16:51

3 Answers3

1

What you're trying to do isn't directly possible under normal circumstances - simply launching an app in a new process from your Windows Service code is not going to interact with the GUI of the currently logged in user I'm afraid.

There are ways of communicating between a service and the GUI however.

This discussion might point you in the right direction.

Community
  • 1
  • 1
olitee
  • 1,683
  • 10
  • 12
  • Hi @olitee Thanks for the reply. I tried using the "Interact with desktop" option. This successfully launches the application, but asks for user permission to read the message or discard. The purpose of the application is to fill in the attendance compulsorily and not postpone it. Is there any possibility where it can override user permission and launch the application directly? – Pavan Kumar Dec 01 '14 at 16:54
  • I'm not aware of any way of any way around this - that default behaviour is there for a reason. The preferred approach is for your service to communicate with a desktop app through named pipes. You could create a helper app that runs silently in the user's session, and monitors that pipe for messages. That app could either present it's GUI to the user when a message is recieved - or launch another app in the way you've already described. – olitee Dec 01 '14 at 16:59
0

Based on your comments, I think what you are really looking for here is a normal userspace application and a scheduler. You might want to use Windows' own scheduler to run the application every monday, if it is an always-on box, or place the application in Startup. When the application runs, it should check the current day of week, and if it is monday and the application has not previously run this day, the application should start. If not, you can safely terminate the application entirely.

  • The scheduler part can be configured by me for my computer right?. But this application has a multiple user base, it has to be pre-configured with this logic. Service had my job done, but i realised it failed only after installed it. – Pavan Kumar Dec 01 '14 at 17:00
0

Thanks for the answers! I found out a solution for it and im posting it here. I created a dummy app which is hidden on startup and it does the exact same function that the service was intended to.

1.create a dummy app (copy paste code from service to form application) Hide it after start up. 2.start the application right after installation. 3.add registry key so that it starts up after a system reboot.

in simple words, clone service behavior.

Pavan Kumar
  • 157
  • 2
  • 4
  • 17