Start WinForms application from Windows Service

萝らか妹 提交于 2019-12-11 01:29:53

问题


I have a (3rd party) WinForms application that I need to keep alive 24/7 on a Windows Server. My first thought was to write a Windows Service that starts the application (using System.Diagnostics.Process) and restarts it if it should crash or the server is restarted.

Is this even possible? I need it to start even if no user is signed in on the server. Can an application with a user interface run when no one is logged in? If no, is there a workaround?

N.B. I don't need to interact with the application in any way, I just need to start it.


回答1:


Why do you need to keep a form running 24/7? Here are some possibilities I can imagine.

  1. You need a specific behavior to occur every so often.
  2. It's opening a port that you want other applications to connect to.
  3. You don't want employees to be able to exit the application.

If the answer is #3 then a Windows Service isn't the right answer and anything you try is just going to turn into a huge hack. If it's #1 or #2 you need to think about separating your single application into two.

For a recurring task the easiest solution would be to develop a console application and setting up a Task in the Windows Task Scheduler. This is much easier to manage and debug than a Windows Service. If you are trying to run a socket service of some kind the typical approach is to create an application with no GUI and the only way to access it is through a socket connection. If the WinForm closes the socket service keeps chugging away and the WinForm can grab the latest information when it is reopened.




回答2:


I would suggest you to think of a different approach. trying to show a GUI to user through windows service is not going to be a easy task, I came accross similar situation and finally i had to go for a different approach.

Why can't you directly show UI from Service : This is due to some security implementation went into windows OS with Windows Vista and later OS versions. Microsoft disallowed windows services from directly interacting with user desktop as it led to some security flaw

-

I don't need to interact with the application in any way

if thats the case, I assume that application doesnt need any user interaction to continue its operation it will do its own ? if then you can even go for a command prompt exe.

I need it to start even if no user is signed in on the server

I would strongly recommend you to go for Windows Task Scheduler. Which will make your life much more easier. it doesnt require any user to manually start the program, It can start your progarm when computer starts/re-starts or even you can schedule to run your program on specific intervals. You can also check other good .net task scheduling libraries, quartznet is one of them.



来源:https://stackoverflow.com/questions/19100751/start-winforms-application-from-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!