我有,我創建的長時間運行的任務列表,監視一些系統/網絡資源的情況,然後發送電子郵件,登錄到一個txt傳播一些數據從TPL任務主要流程文件和在滿足某些條件時調用Web服務。然後再次開始監測。這些任務是在Windows服務中創建的,因此會一直運行。如何當任務運行時
我希望他們引發事件或通知父類(創建它們),它將執行我上面提到的3個操作,而不是執行它自己的任務中的每個對象。
如何控制只有一個任務在同一時間只使用該父類的方法。由於涉及電子郵件和Web服務調用,因此兩個併發請求可能會導致代碼崩潰。
UPDATE
這些看守有三種類型,各實現以下接口。
public interface IWatcher
{
void BeginWatch();
}
類實現是
//this watcher is responsible for watching over a sql query result
public class DBWatcher : IWatcher
{
....
void BeginWatch()
{
//Here a timer is created which contiously checks the SQL query result.
//And would Call SERVICE, send an EMAIL and LOG into a file
Timer watchIterator = new Timer(this._intervalMinutes * 60000);
watchIterator.Elapsed += new ElapsedEventHandler(_watchIterator_Elapsed);
watchIterator.Start();
}
void _watchIterator_Elapsed(object sender, ElapsedEventArgs e)
{
//1. Check Query result
//3. Call SERVICE, send an EMAIL and LOG into a file if result is not as was expected
//I have done the work to this part!
//And I can do the functions as follows .. it should be simple.
//*********************
//SendEmail();
//LogIntoFile();
//CallService();
//But I want the above three methods to be present in one place so i dont have to replicate same functionality in different watcher.
//One approach could be to create a seperate class and wrape the above mentioned functions in it, create an instance of that class here and call them.
//Second option, which I am interested in but dont know how to do, is to have this functionality in the parent class which actually creates the tasks and have each watcher use it from HERE ...
}
....
}
//this watcher is responsible for watching over Folder
public class FolderWatcher : IWatcher
{
....
void BeginWatch()
{
///Same as above
}
....
}
首先我創建一個XML文件列表。這可以包含DBWatcher的多個實例,它將持續觀看不同的查詢結果和FolderWatcher,它將連續不斷地觀察不同的文件夾。
List創建完成後,我調用以下函數創建一個單獨的Task。我多次調用這個函數來創建一組不同的觀察者。
在CallWebService(消息)執行後,這個任務不會停止嗎?在我的情況下,我不希望任務被阻止。它將永遠運行。和thanx的鏈接。我會讀它。 – Aamir 2013-05-09 16:53:58
是的,這個任務會在之後停止。我編輯了我的答案以提供一個無限運行的版本。 – Chris 2013-05-09 17:39:17
非常感謝您的解釋......我編輯了我的問題,包含了一些代碼片段,介紹了我在做什麼/如何做...這將使您清楚地瞭解我需要什麼。真的很抱歉沒有提供此之前......我應該有 – Aamir 2013-05-09 19:14:31