如何将 Windows GUI 应用程序作为服务运行?

问题描述:

我有一个应该作为服务实现的现有 GUI 应用程序.基本上,我需要能够远程登录和注销 Windows 2003 服务器,并且仍然保持该程序运行.

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.

这甚至可能吗?

这里进一步细化...我没有源,这不是我的应用程序.

Further refinement here... I do not have the source, it's not my application.

Windows 服务不能有 GUI,所以你需要要么去掉 GUI,要么把你的应用程序分成两部分——一个没有 UI 的服务,一个控制器"应用程序.如果您有源代码,那么将非 GUI 代码转换为服务很容易 - Visual Studio 有一个Windows 服务"项目类型可以为您处理包装,并且有一个简单的演练向您展示如何创建负责安装的部署项目.

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.

如果您选择第二条路线并需要将一些原始 GUI 代码放入控制器,则控制器和服务可以通过 WCF、.NET Remoting 或普通套接字连接使用您自己定义的协议进行通信.如果您使用 Remoting,请务必使用笨重"的接口,以尽可能少的方法调用来传输数据 - 每次调用都有相当多的开销.

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.

如果 UI 相当简单,您可以使用配置文件作为输入和日志文件或使用 Windows 事件日志作为输出.

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.