我有一個奇怪的問題,我沒有見過,我認爲它必須是簡單的東西,我沒有在我的代碼中看到。單個項目中的多個窗口服務=神祕
我有一個定義了2個窗口服務的項目。一個我叫DataSyncService,另一個SubscriptionService。兩者都被添加到相同的項目安裝程序。兩者都使用System.Timers中的計時器控件。
如果我一起啓動兩項服務,它們似乎工作正常。定時器在適當的時間流逝,一切看起來都不錯。但是,如果我單獨啓動其中一項服務,而另一項服務停止,則一切都會失控。定時器不斷流逝,並在錯誤的服務。換句話說,如果我啓動DataSyncService,SubscriptionService計時器會一遍又一遍地流逝。這顯然很奇怪。
該設置與我在過去所做的類似,所以我真的難倒了。我甚至嘗試刪除這兩個服務並重新開始,但它似乎沒有什麼區別。在這一點上,我認爲我在定義服務的方式上犯了一個簡單的錯誤,而我的大腦不會讓我看到它。它必須創建某種線程問題,導致一個服務在另一個服務停止時競爭。下面的代碼....
從Program.cs的:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new DataSyncService(),
new SubscriptionService()
};
ServiceBase.Run(ServicesToRun);
}
從ProjectInstaller.designer.cs:
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.dataSyncInstaller = new System.ServiceProcess.ServiceInstaller();
this.subscriptionInstaller = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// dataSyncInstaller
//
this.dataSyncInstaller.DisplayName = "Data Sync Service";
this.dataSyncInstaller.ServiceName = "DataSyncService";
this.dataSyncInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// subscriptionInstaller
//
this.subscriptionInstaller.DisplayName = "Subscription Service";
this.subscriptionInstaller.ServiceName = "SubscriptionService";
this.subscriptionInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.dataSyncInstaller,
this.subscriptionInstaller});
}
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller dataSyncInstaller;
private System.ServiceProcess.ServiceInstaller subscriptionInstaller;
從DataSyncService.cs:
public static readonly int _defaultInterval = 43200000;
//log4net.ILog log;
public DataSyncService()
{
InitializeComponent();
//log = LogFactory.Instance.GetLogger(this);
}
protected override void OnStart(string[] args)
{
timer1.Interval = _defaultInterval; //GetInterval();
timer1.Enabled = true;
EventLog.WriteEntry("MyProj", "Data Sync Service Started", EventLogEntryType.Information);
//log.Info("Data Sync Service Started");
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("MyProj", "Data Sync Timer Elapsed.", EventLogEntryType.Information);
}
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// DataSyncService
//
this.ServiceName = "DataSyncService";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
從SubscriptionService:
public static readonly int _defaultInterval = 300000;
//log4net.ILog log;
public SubscriptionService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Interval = _defaultInterval; //GetInterval();
timer1.Enabled = true;
EventLog.WriteEntry("MyProj", "Subscription Service Started", EventLogEntryType.Information);
//log.Info("Subscription Service Started");
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
EventLog.WriteEntry("MyProj", "Subscription Service Time Elapsed", EventLogEntryType.Information);
}
private void InitializeComponent() //in designer
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// SubscriptionService
//
this.ServiceName = "SubscriptionService";
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
}
同樣,問題是timer1_elapsed處理程序在只有其中一個服務啓動時持續運行。它是OPPOSITE服務的處理程序。
有人看到什麼嗎?
什麼Program.cs中做什麼?我的意思是,我可以看到它啓動了兩項服務,但它何時運行? – SqlRyan 2010-05-12 15:31:46
當你向這個世界釋放這樣的東西時,這個答案只會讓你尷尬,這不是太神奇了嗎?在Service.Designer.cs文件的InitializeComponent()方法中,我很缺少 this.CanShutdown = true; ...我不應該啓用計時器,因爲我在OnStart處理程序中執行它。 噢,最終其他人會有同樣的問題,希望他們會找到這個。 – Remoh 2010-05-12 15:32:55