如何将 Windows GUI 应用程序作为服务运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a Windows GUI application on as a service?
提问by JeffV
I have an existing GUI application that should have been implemented as a service. Basically, I need to be able to remotely log onto and off of the Windows 2003 server and still keep this program running.
我有一个应该作为服务实现的现有 GUI 应用程序。基本上,我需要能够远程登录和注销 Windows 2003 服务器并仍然保持该程序运行。
Is this even possible?
这甚至可能吗?
EDIT: Further refinement here... I do not have the source, it's not my application.
编辑:这里进一步细化......我没有来源,这不是我的应用程序。
回答by McKenzieG1
Windows services cannot have GUIs, so you will need to either get rid of the GUI or separate your application into two pieces - a service with no UI, and a "controller" application. If you have the source code, converting the non-GUI code into a service is easy - Visual Studio has a 'Windows Service' project type that takes care of the wrapping for you, and there is a simple walkthrough that shows you how to create a deployment project that will take care of installation.
Windows 服务不能有 GUI,因此您需要摆脱 GUI 或将您的应用程序分成两部分 - 一个没有 UI 的服务和一个“控制器”应用程序。如果您有源代码,那么将非 GUI 代码转换为服务很容易 - Visual Studio 有一个“Windows 服务”项目类型可以为您处理包装,并且有一个简单的演练向您展示如何创建一个负责安装的部署项目。
If you opt for the second route and need to put some of the original GUI code into a controller, the controller and service can communicate via WCF, .NET Remoting or plain socket connections with a protocol you define yourself. If you use Remoting, be sure to use a "chunky" interface that transfers data with as few method invocations as possible - each call has a fair amount of overhead.
如果您选择第二条路线并需要将一些原始 GUI 代码放入控制器中,则控制器和服务可以通过 WCF、.NET Remoting 或普通套接字连接与您自己定义的协议进行通信。如果您使用远程处理,请确保使用“笨重”的接口,该接口通过尽可能少的方法调用来传输数据——每次调用都有相当多的开销。
If the UI is fairly simple, you may be able to get away with using configuration files for input and log files or the Windows Event Log for output.
如果 UI 相当简单,您可以避免使用配置文件作为输入和日志文件,或者使用 Windows 事件日志作为输出。
回答by JeffV
Has anyone used a third party product like: Always Up?
有没有人使用过第三方产品,例如:Always Up?
Seems to do what I need. It's the capability to keep running through login / logout cycles I need. And the capability to ignore that it's a GUI app and run it anyway.
似乎做我需要的。这是在我需要的登录/注销周期中保持运行的能力。并且能够忽略它是一个 GUI 应用程序并无论如何都运行它。
They must be linking into the exe manually and calling WinMain or something.
他们必须手动链接到 exe 并调用 WinMain 或其他东西。
回答by Mark Brackett
回答by joshua
Do you actually need it to run as a service or do you just need it to stay running when you aren't connected? If the latter, you can disconnect instead of logging off and the application will continue running. The option should be in the drop down list after choosing Shut Down or you can call tsdiscon.exe.
您真的需要它作为服务运行还是只需要它在未连接时保持运行?如果是后者,您可以断开连接而不是注销,应用程序将继续运行。选择关机后,该选项应该在下拉列表中,或者您可以调用 tsdiscon.exe。
回答by VitalyB
I've had good experience with winsw. I was able to convert quite easily my batch files to services using it.
我在winsw 方面有很好的经验。我能够很容易地将我的批处理文件转换为使用它的服务。
I've used it for nginx as well, per this answer.
根据这个答案,我也将它用于 nginx 。
回答by IneedHelp
FireDaemonProturns most GUI apps into services; it's not free, but it might be worth getting it.
FireDaemonPro将大多数 GUI 应用程序转换为服务;它不是免费的,但可能值得购买。
回答by labilbe
You can use ServiceMillto achieve this operation. Basically you install ServiceMill Server on your server. Then click on right button over your executable file and "Install as a ServiceMill Service". Next you configure some things (user/password, if you want to interact with desktop or if you prefer to hide the ui... and set the start mode to automatic).
您可以使用ServiceMill来实现此操作。基本上,您在服务器上安装 ServiceMill Server。然后单击可执行文件上的右键并“安装为 ServiceMill 服务”。接下来你配置一些东西(用户/密码,如果你想与桌面交互,或者如果你更喜欢隐藏用户界面......并将启动模式设置为自动)。
Another tool from Active+ Software can be a solution, ServiceMill Exe Builderwhich allows you to create services from Command Line and this is great if you are using a Continuous Integration Server or if you plan to distribute your component as a service without having to think about service integration (plus it is royalty free).
Active+ Software 的另一个工具可以是一个解决方案,ServiceMill Exe Builder,它允许您从命令行创建服务,如果您使用持续集成服务器或者如果您计划将组件作为服务分发而无需考虑服务集成(另外它是免版税的)。
回答by Aardvark
What happens if you create a service. That service is configure to interact with the desktop. Configure it to run a some user and to start automatic. From the service CreateProcess on this other application. I'd guess this is quick to try using C# (C/C++ was alot of code to even be a service if I recall). Would that work??
如果您创建服务会发生什么。该服务配置为与桌面交互。将其配置为运行某个用户并自动启动。来自此其他应用程序上的服务 CreateProcess。我猜想这会很快尝试使用 C#(如果我记得的话,C/C++ 有很多代码甚至可以作为服务)。那行得通吗??
BUT!
但!
My first thought would be to create a virtual computer in a server-class virtual host (like Virtual Server, HyperV, VMWare). Those virtual machines will run as service (or whatever Hyper V does). The virtual machine would always be running - regardless of logging in and out.
我的第一个想法是在服务器级虚拟主机(如 Virtual Server、HyperV、VMWare)中创建一台虚拟计算机。这些虚拟机将作为服务运行(或 Hyper V 所做的任何事情)。虚拟机将始终运行 - 无论登录和退出。
Make this virtual computer auto login to windows (TweakUI can set this up) and then just launch the GUI app using a shortcut to the Startup folder. You can even remote desktop into it use the program's GUI (I bet Always Up can't do that).
使这台虚拟计算机自动登录到 Windows(TweakUI 可以设置它),然后使用 Startup 文件夹的快捷方式启动 GUI 应用程序。你甚至可以使用程序的 GUI 远程桌面进入它(我打赌 Always Up 不能这样做)。
回答by Brad Bruce
Do you have the source? In many cases the difference between a stand alone application and a service are minimal.
你有源码吗?在许多情况下,独立应用程序和服务之间的区别很小。
Most of the changes are related to hooking the code into the service manager properly. Once done, you'll know that any problems that occur are a result of your programming and not any other program.
大多数更改与将代码正确挂接到服务管理器有关。一旦完成,您就会知道发生的任何问题都是您的编程而不是任何其他程序的结果。
回答by Craig Tyler
First I would have to ask why your service needs a user interface. Most likely it does not but you probably need a client that gets data from this service. The reason services don't usually have GUI's is they may not have a window environment to run in. Services can start and run without a user logged in to the machine. In this case there would be no desktop for the service GUI to run in.
首先,我必须问为什么您的服务需要用户界面。很可能没有,但您可能需要一个从该服务获取数据的客户端。服务通常没有 GUI 的原因是它们可能没有运行的窗口环境。服务可以在没有用户登录到机器的情况下启动和运行。在这种情况下,将没有用于运行服务 GUI 的桌面。
Having said that you can set properties on the service to run as a user as suggested by Mark. You can also specify in the properties of the service to "Allow service to interact with desktop". Only do this if you know a user will be logged in.
话虽如此,您可以按照 Mark 的建议将服务的属性设置为以用户身份运行。您还可以在服务的属性中指定“允许服务与桌面交互”。仅当您知道用户将登录时才执行此操作。