简体   繁体   中英

Run Windows Forms application as service?

I have a Windows Forms application that I wrote that does some monitoring of an inbox and database. The customer has informed me that it needs to run every time the server reboots. Shame on me for letting the customer change the requirements.

I was thinking ... is there any way to make a Windows Forms application run as a service so that it starts automatically?

Clarification

I would like to try to not have to write any more code ... if possible!

(This answer is obsolete, since the linked article was deleted in 2012. However, StackOverflow does not allow the accepted answer to be deleted. So, kindly do not downvote this answer, since I have informed you that it is obsolete.)

Alt link: https://web.archive.org/web/20111221041348/http://www.codeproject.com/kb/system/xyntservice.aspx

C++ source (Or use the Browse Code tab on the article): https://web.archive.org/web/20111228050501/http://www.codeproject.com/KB/system/xyntservice.aspx

Unfortunately, the Zip file source code download does not work within the alternative links.

Start Your Windows Programs From An NT Service

Check this article out: http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=719&av=49654

It's a really old article, but it has worked for 8 years and it continues to be kept up to date by the author.

It will do what you want to do.

If you are sure the application can run unattended safely (by this I mean it can never throw up a modal UI element like a Message Box) and it doesn't need any interaction until shutdown, where it will simply be terminated, then run it as a scheduled task with the trigger set to system start up.

If it can run unattended but it may need to be shutdown and restarted manually or it can't just be terminated at shutdown then use the XYNTService as recommended by EnocNRoll . It's a horrible hack but it will work for what you want.

But by far the best solution is to separate the functionality of your program from the User Interface and write a proper service. And for a production server I wouldn't allow anything else. If it isn't easy to separate then you have some design issues you should look into anyway.

You may also want to consider AlwaysUp , a commercial application that will run any application as a Windows Service. It is similar to XYNTService (mentioned by EnocNRoll), but has more features and is fully supported.

Good luck!

您可以将 winforms 应用程序作为服务运行,但您将无法看到它——它将显示在所谓的虚拟桌面上,而无法在您的监视器上查看。

The only way to even consider this is to make sure that the application has no UI elements to it, as you have to jump through hoops on a non-Vista machine to make this work, and on Vista, you can't interact with the desktop at all.

Rather, you would be best off refactoring out the functionality into a set of shared libraries, and then create a service that uses those libraries, and install that at the client.

If you have a nice decoupled functionality in you forms application, it should be straightforward to create a service class with its installer and then launch your processor class in the OnStart method of the service:

    protected override void OnStart(string[] args)
    {
                    Processor processor = new Processor();
        Thread workerThread = new Thread(processor.OnStart);
        workerThread.IsBackground = true;

        try
        {
            workerThread.Start();
        }
        catch
        {
                      //...
        }
    }

您可以使用InstallUtil将您的应用程序安装为服务,但您需要确保它不依赖于 GUI,我建议您更改应用程序的启动,以便它不会尝试创建任何表单。

找到了这篇名为How To Create a User-Defined Service 的文章

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