2013-10-09 11 views
1

我使用Google搜索this one自我安裝的WInService

但是當我試圖應用它時,我得到一個錯誤。所以安裝/卸載工作正常,但服務本身不啓動,並在超時後說服務沒有響應。我不知道爲什麼。當我附加處理它甚至不進入Main()方法,靜態構造函數等等。我已使用this addon進行附加。

public static void Main() 
    { 
     AppDomain.CurrentDomain.UnhandledException += OnException; 

     if (Environment.UserInteractive) 
     { 
      AskUserForInstall(); 
     } 
     else 
     { 
      ServiceBase.Run(new NotificatorService()); 
     } 
    } 

服務也很簡單:

using System.ServiceProcess; 
using System.Windows; 

namespace AZNotificator 
{ 
    public partial class NotificatorService : ServiceBase 
    { 
     static NotificatorService() 
     { 
      int x = 5; 
     } 
     public NotificatorService() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      MessageBox.Show("Hello"); 
     } 

     protected override void OnStop() 
     { 
     } 
    } 
} 

回答

2

您無法從Windows服務調用MessageBox.Show("Hello");,因爲服務沒有GUI。

如果你想這樣做從Windows服務的一些交互在本文

http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx

看看所以從您的OnStart方法去除MessageBox.Show("Hello");和您的服務應該啓動就好了。