简体   繁体   中英

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. Currently it is a Windows Forms application with GUI set for debugging (shows who has connected and some other messages).

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). The service would start a server by creating a new 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?

( 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) ).

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. Then have a separate Windows Forms app just for debugging. This app can send info requests to the Windows service on a different port, and display the results.

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. In Vista and above, no interactive user session has access to session 0.

You could use the CreateProcessAsUser API function to create a process on a specific user's desktop. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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