简体   繁体   中英

In C#, can you make a windows form application start as a service?

I have a GUI C# Windows form program that I would like to start as a service when the computer starts and perhaps have an icon in the system tray that when clicked maximizes the program. Is this possible without major rework?

Thanks

Windows Services cannot have a GUI, at least not directly. You will have to separate your application to a presentation layer/process and a service layer/process:

  • The presentation layer will remain a WinForms application
  • The service layer will run as a Windows Service

The two of them will have to communcate with each other with some means of inter-process communcation, like named pipes or sockets.

You can use a third party app, such as FireDaemon ( http://www.firedaemon.com/ ), to start any program as a service. There are many options available in FireDaemon, such as form visibility, restart on failure, etc. However, it will not automatically create a tray icon for your app. So your app will have to be changed to have its own tray icon functionality and FireDaemon will just start the program and manage the process.

FireDaemon costs about $40 (USD). I imagine there are many other similar applications available.

I would first look into creating an actual service project as mentioned by other answerers, but keep this approach in mind. It has worded well for me in a handful of situations.

It depends on how the code is currently written. I have several WinForm apps that double as services, but the bulk of the work I have separated into another assembly. My solutions for those apps generally have 3 projects: WinApp, Service, and Library (I'm oversimplifying here).

If you feel that your WinForm app could make a good service then you probably have your code in such a state that you could probably separate it out easily enough. Adding a service project is pretty simple, adding the installer for it is a little more challenging but still well documented. The trickiest part is making a deployment package for it that installs the service properly, but again... its well documented as long as you know you need to look for it.

Edit: Just to clarify, in general I wouldn't consider this a major project.

You can write the code to have it run as a service, but I think the more important question is, what does it provide? There are ways of minimizing an application to the tray, and you can start said applications at launch to the system tray.

This is the link that I always refer back to about doing windows services. It is WCF based, but I think with a little modification you could make it work for you:

http://support.microsoft.com/kb/317421

As to minimizing to a tray, there's an excellent answer in this question:

What's the proper way to minimize to tray a C# WinForms app?

您可以在Windows中使用任务管理器并设置一个任务,该任务将执行您的应用程序的.exe每个Windows启动。

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