mattruma
mattruma

Reputation: 16677

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!

Upvotes: 6

Views: 17502

Answers (7)

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

Upvotes: 9

CoreTech
CoreTech

Reputation: 2433

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!

Upvotes: 1

Stephen Martin
Stephen Martin

Reputation: 9645

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.

Upvotes: 5

MatthewMartin
MatthewMartin

Reputation: 33143

You can run a winforms application as a service, you just won't be able to see it-- it will be displayed on a so-called virtual desktop, which can't be viewed on your monitor.

Upvotes: 1

Steven Robbins
Steven Robbins

Reputation: 26599

You can use InstallUtil to install your app as a service, but you need to make sure it's not reliant on the GUI, and I would recommend you change the startup of the app so it doesn't attempt to create any forms.

Upvotes: 0

Ricardo Villamil
Ricardo Villamil

Reputation: 5107

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
        {
                      //...
        }
    }

Upvotes: 0

casperOne
casperOne

Reputation: 74530

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.

Upvotes: 0

Related Questions