简体   繁体   English

从Windows服务启动exe应用程序

[英]Launch an exe application from windows service

I have a service which should open an exe application ( C# Application ) based on certain conditions. 我有一项服务,应根据某些条件打开exe应用程序(C#Application)。 When the service is started in debugging mode ( Visual studio ) it opens the application. 当服务以调试模式(Visual Studio)启动时,它将打开应用程序。 But when it is installed as a service, it does not do so. 但是,当将其作为服务安装时,就不会这样做。 It fails to open the application. 无法打开应用程序。 Why is this happening? 为什么会这样呢?

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. 在正常情况下,您尝试做的事情是不可能直接实现的-恐怕只是从Windows服务代码以新进程启动应用程序就不会与当前登录用户的GUI交互。

There are ways of communicating between a service and the GUI however. 但是,有一些方法可以在服务和GUI之间进行通信。

This discussion might point you in the right direction . 讨论可能会为您指明正确的方向

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. 您可能想要使用Windows自己的调度程序,如果它始终处于打开状态,则每个星期一都运行该应用程序,或者将其放置在“启动”中。 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. 如果没有,您可以完全终止该应用程序。

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. 1.创建一个虚拟应用程序(从服务复制粘贴代码到表单应用程序)在启动后将其隐藏。 2.start the application right after installation. 2.安装后立即启动应用程序。 3.add registry key so that it starts up after a system reboot. 3.添加注册表项,以便在系统重新引导后启动。

in simple words, clone service behavior. 简单来说,就是克隆服务行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM