2012-04-20 214 views
0

我已經創建了一個windows服務併爲其創建了安裝程序。它已安裝並啓動,但我寫入其中的代碼未執行。實際上,當我從服務窗口啓動服務時,OnStart()函數未被觸發。也沒有初始化組件(),也沒有靜態無效的主要功能..任何一個可以幫助我吧安裝了windows服務但不工作

請指導我在哪裏做錯了。

這裏有一些代碼行。讓我知道如果u想要更多的東西我已經寫

public partial class iMArchiveService : ServiceBase 
{ 
    Boolean isArchiving = false; 

    public iMArchiveService() 
    { 
     MyException.CreateLog("iMArchiveService: Inside Constructor. Initializing Component"); 
     InitializeComponent(); 
     MyException.CreateLog("iMArchiveService: Component Initialized. Timer is set as: " + TimeMachine.Interval.ToString() + " milliseconds"); 
    } 

    protected void TimeMachine_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     try 
     { 
      MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable before condition is: " + isArchiving.ToString()); 
      if (!isArchiving) 
       isArchiving = new iM.OrderArchiving.ArchiveOrderXML().ArchiveOrderService(); 
      MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable after condition is: " + isArchiving.ToString()); 
     } 
     catch (Exception ex) 
     { 
      MyException.CreateLog("iMArchiveService: Inside Tick Catch :("); 
     } 
    } 

    protected override void OnStart(string[] args) 
    { 
     TimeMachine.Enabled = true; 
     MyException.CreateLog("iMArchiveService Started: " + DateTime.Now.ToString()); 
    } 

    protected override void OnStop() 
    { 
     TimeMachine.Enabled = false; 
     MyException.CreateLog("iMArchiveService Stopped: " + DateTime.Now.ToString()); 
    } 

} 

上面的代碼是服務file.cs

這裏是我的項目安裝文件

namespace iM.OrderArchivingService 
{ 
    [RunInstaller(true)] 
    public partial class ProjectInstaller : Installer 
    { 
     public ProjectInstaller() 
     { 
     InitializeComponent(); 
     } 
    } 
} 

這裏是InitializeComponenet功能 -

private void InitializeComponent() 
    { 
     this.myServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller(); 
     this.myServiceInstaller = new System.ServiceProcess.ServiceInstaller(); 
     // 
     // myServiceProcessInstaller 
     // 
     this.myServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
     this.myServiceProcessInstaller.Installers.AddRange(new System.Configuration.Install.Installer[] { 
     this.myServiceInstaller}); 
     this.myServiceProcessInstaller.Password = null; 
     this.myServiceProcessInstaller.Username = null; 
     // 
     // myServiceInstaller 
     // 
     this.myServiceInstaller.ServiceName = "iMArchiveService"; 
     // 
     // ProjectInstaller 
     // 
     this.Installers.AddRange(new System.Configuration.Install.Installer[] { 
     this.myServiceProcessInstaller}); 

    } 

這裏是program.cs文件

namespace iM.OrderArchivingService 
{ 
static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    static void Main(string[] args) 
    { 
     ServiceBase[] ServicesToRun; 
     ServicesToRun = new ServiceBase[] { new iMArchiveService() }; 
     ServiceBase.Run(ServicesToRun); 
    } 
} 
} 

因爲你看我已經寫了代碼登錄時初始化或開始..但沒有日誌正在作出。

編輯:

代碼定時器(時間機器)

private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.TimeMachine = new System.Timers.Timer(3600000); 
     // 
     // TimeMachine 
     // 
     this.TimeMachine.Interval = 3600000; 
     this.TimeMachine.Elapsed += new System.Timers.ElapsedEventHandler(TimeMachine_Elapsed); 
     // 
     // iMArchiveService 
     // 
     this.ServiceName = "iMArchiveService"; 

    } 

日Thnx

+0

TimeMachine計時器的間隔是多少? – Coder 2012-04-20 06:23:46

+0

添加了計時器代碼..但至少它應該寫入我開始編碼的日誌。該日誌也不寫爲 – 1Mayur 2012-04-20 07:05:48

+0

我猜它不是計時器問題...日誌記錄進一步顯示它到達projectinstaller初始化組件函數,但它沒有得到什麼必須開始。我的意思是我的初始化函數需要引用我的服務基類... – 1Mayur 2012-04-20 09:35:56

回答

1

您使用了錯誤的Timer類 - 的線索是它的命名空間:System.Windows.Forms.Timer。該計時器僅適用於WinForms應用程序。

你應該,而是改用System.Timers.Timer


還有就是定時器類的System.Threading.Timer了一般性討論:

System.Threading.Timer是使用回調方法和一個簡單的,輕量級的計時器由線程池線程提供服務。不建議用於Windows窗體,因爲它的回調不會在用戶界面線程上發生。 System.Windows.Forms.Timer是與Windows Forms一起使用的更好選擇。對於基於服務器的定時器功能,您可能會考慮使用System.Timers.Timer,這會引發事件並具有其他功能。

(我強調代替原件)

+0

讓我們試試這種方式 – 1Mayur 2012-04-20 08:09:33

+0

試過..仍然沒有成功 – 1Mayur 2012-04-20 08:26:28

0

也許你應該使用Timer.Start()和Timer.Stop()方法來啓動和停止定時器,以防萬一使用Enabled屬性時出現問題。

間隔時間爲3600秒,即3600秒或60分鐘= 1小時。直到一個小時過去,什麼都不會發生;這是你的意圖?

BTW,設置像下面的例子中的間隔將使你的代碼變得更容易閱讀:

this.TimeMachine.Interval = 1 * 1000; // 1 Second 
this.TimeMachine.Interval = 60 * 1000; // 60 Seconds 
this.TimeMachine.Interval = 60 * 60 * 1000; // 1 hour 

嘗試使用中的Debug.WriteLine()方法在System.Diagnostics程序。默認情況下,將在MSVS的輸出窗口中發佈消息。你也會看到有任何異常。