且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows服务中显示窗体表单应用程序?

更新时间:2023-10-21 13:31:16

Windows服务没有应用程序消息泵,因此无法显示任何图形用户界面。

因此,服务似乎不是解决方案。



你有几种选择:

- 你保留你的用户界面但不能把它作为服务运行。

- 你扔UI离开,只保留功能部分(服务必须完成的任务)。

- 您保留实际应用程序并开发第二个项目,该项目将作为服务运行。
A Windows service has no application message pump, thus it is unable to display whatever graphical UI.
So a service does not seem to be the solution here.

You have several choices:
- you keep your UI but won't be able to run it as service.
- you throw the UI away, only keeping the functional part (what the service will have to accomplish).
- you keep your actual application and develop a second project that will be the program that is run as a service.


您无法将交互式应用程序(Windows窗体)作为服务运行,因为它只会坐在那里等待永远不会发生的用户输入。为什么?因为作为服务运行的应用程序是在用户看不到的桌面下运行的。



由于应用程序将启动,您也无法从服务应用程序启动交互式应用程序在服务桌面上,而不是用户。



要在Windows启动时启动(在任何人登录控制台之前),你必须将应用程序重写为服务应用程序,而不是Windows窗体。根本没有任何用户界面。



如果您需要与服务进行用户交互,请编写一个单独的Windows窗体应用程序,用户可以启动该应用程序与之通信服务通过一些进程间通信方法,如命名管道。
You cannot run an interactive application (Windows Forms) as a service as it will just sit there waiting for user input that will never happen. Why? Because applications running as a service are running under a desktop separate that the user cannot see.

You also cannot launch an interactive application from a service application as the application will launch on the service desktop, not the users.

To launch at Windows startup (before anyone logs into the console), you MUST rewrite the application as a service app, not Windows Forms. There cannot be any user interface at all.

If you need to have user interaction with the service, write a separate Windows Forms application that the user can launch that communicates with the service through some of Inter-Process Communication method, such as named pipes.