简体   繁体   English

Windows服务调用Windows窗体应用程序-GUI会引起问题吗?

[英]Windows service calling a Windows Forms application - would GUI cause problems?

I have a server application Server.exe that listens on a TCP port and replies to clients that connect to it. 我有一个服务器应用程序Server.exe ,它在TCP端口上侦听并回复连接到它的客户端。 Currently it is a Windows Forms application with GUI set for debugging (shows who has connected and some other messages). 当前,它是一个Windows窗体应用程序,具有用于调试的GUI(显示谁已连接以及其他消息)。

I would like to make a Windows Service that will start this Server.exe and also ensure that the Server.exe is always running (restarting it if it crashes for any reason). 我想制作一个Windows服务,它将启动此Server.exe ,并确保Server.exe始终运行(如果由于任何原因而崩溃,请重新启动它)。 The service would start a server by creating a new Process (of Server.exe ). 该服务将通过创建一个新的Process (of Server.exe )启动服务器。 The service can be run (and usually is) when no users are logged in. Since the Server.exe does have some GUI, would this create any problems in scenario in which no user is logged in? 当没有用户登录时,可以运行该服务(通常是运行该服务)。 由于Server.exe确实具有一些GUI,在没有用户登录的情况下,这会造成任何问题吗?

( I don't care that the GUI won't be visible, I just want to be sure that having GUI does not prevent the app from working properly in context where GUI is not available/visible (ie no user is logged in) ). 我不在乎GUI是不可见的,我只是想确保拥有GUI不会阻止应用在GUI不可用/不可见(即没有用户登录)的情况下正常运行) ) 。

Note: The application is not dependent on any specific user input, and it also has some long-running background threads (which also do not depend on user input). 注意:该应用程序不依赖于任何特定的用户输入,它还具有一些长时间运行的后台线程(也不依赖于用户输入)。

Rather than have your design driven by the info/debugging requirements, it might be better if your Windows service itself does all of the work such as port listening and responding. 如果您的Windows服务本身完成所有工作(例如端口侦听和响应),则可能会更好,而不是由信息/调试要求来驱动您的设计。 Then have a separate Windows Forms app just for debugging. 然后有一个单独的Windows Forms应用程序仅用于调试。 This app can send info requests to the Windows service on a different port, and display the results. 此应用程序可以在其他端口上向Windows服务发送信息请求,并显示结果。

If you have to stick with the current approach, then you have an issue. 如果您必须坚持当前的方法,那么就会遇到问题。 The gui process would by default start in session 0. The GUI would display okay, but nobody would ever see it. 默认情况下,gui进程将从会话0开始。GUI会显示正常,但没人会看到它。 In Vista and above, no interactive user session has access to session 0. 在Vista及更高版本中,没有交互式用户会话可以访问会话0。

You could use the CreateProcessAsUser API function to create a process on a specific user's desktop. 您可以使用CreateProcessAsUser API函数在特定用户的桌面上创建一个进程。 But that would only work if that specific user is logged-in. 但这仅在该特定用户登录后才有效。 And note the caveats stated in that link about user interactions. 并注意该链接中有关用户交互的注意事项。

It used to be possible to create an Interactive Service for Windows, however it is strongly discouraged by Microsoft . 过去可以为Windows创建一个交互式服务,但是Microsoft强烈建议不要这样做 There is a checkbox on LogOn tab of the service property dialog. 服务属性对话框的“登录”选项卡上有一个复选框。

The major issue with creating an interactive service is that it is possible to lock the service waiting for user input that will never occur. 创建交互式服务的主要问题是可以锁定该服务以等待永远不会发生的用户输入。

However if you want to persue this route then this is probably a good starting point. 但是,如果您想说服这条路线,那么可能是一个很好的起点。

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

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