2010-07-12 110 views
7

我有一个简单的c#应用程序需要作为服务运行。我如何使它作为服务运行而不是作为可执行文件运行?c# - 我如何使应用程序作为服务运行?

+0

可能重复[使用c#创建Windows服务的资源](http://stackoverflow.com/questions/1688382/resources-on-creating-a-windows-service-using-c) – Joe 2010-07-12 20:19:52

+0

在这里,我已经找到了分步说明:https://stackoverflow.com/a/593803/7713750 – Rekshino 2017-10-10 07:47:13

回答

2

Visual C# 2010 Recipies有一个例子,它会告诉你如何做到这一点,我试过使用VS 2008和.NET 3.5。

它相当于此:

  1. 在Visual Studio中的一个新的“Windows服务”应用
  2. 端口应用程序的源到服务的执行模型,AKA您的主要功能成为触发的事件处理程序的一部分由定时器对象或东西沿着这些线路
  3. 添加服务安装程序类,您的Windows服务项目 - 它会看起来像下面这段代码片段:

    [RunInstaller(true)] 
    public partial class PollingServiceInstaller : Installer 
    { 
        public PollingServiceInstaller() 
        { 
         //Instantiate and configure a ServiceProcessInstaller 
         ServiceProcessInstaller PollingService = new ServiceProcessInstaller(); 
         PollingService.Account = ServiceAccount.LocalSystem; 
    
         //Instantiate and configure a ServiceInstaller 
         ServiceInstaller PollingInstaller = new ServiceInstaller(); 
         PollingInstaller.DisplayName = "SMMD Polling Service Beta"; 
         PollingInstaller.ServiceName = "SMMD Polling Service Beta"; 
         PollingInstaller.StartType = ServiceStartMode.Automatic; 
    
         //Add both the service process installer and the service installer to the 
         //Installers collection, which is inherited from the Installer base class. 
         Installers.Add(PollingInstaller); 
         Installers.Add(PollingService); 
        } 
    } 
    

最后你会使用一个命令行工具来实际安装服务 - 你可以看到它是如何工作在这里:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d8f300e3-6c09-424f-829e-c5fda34c1bc7

让我知道如果您有任何问题。

3

在visual studio中有一个名为“Windows Service”的临时文件。如果您有任何问题要告诉我,我会整天写信。

+0

非常感谢乔纳森,我一定会带你 – 2010-07-12 20:23:28

相关问题