2010-07-12 72 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

相關問題