2013-05-09 65 views
0

我有,我創建的長時間運行的任務列表,監視一些系統/網絡資源的情況,然後發送電子郵件登錄到一個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。我多次調用這個函數來創建一組不同的觀察者。

回答

2

只要你讓你的電子郵件,登錄& Web服務調用線程安全的,你可以通過引用它發送到每個匯作爲一個封閉的代碼(這裏是C#倒閉喬恩斯基特的excellent explanation)納入監測任務。在這裏你需要啓動多個任務的例子:

... 
void Email(string message){} 

void Log(string message){} 

void CallWebService(string message){} 


void RunMonitoringTask() 
{ 
    var task = Task.TaskFactory.StartNew(() => 
    { 
     string message = Monitor(); 
     if(ShouldNotify(message)) 
      { 
       Email(mesasge); 
       Log(message); 
       CallWebService(message); 
      } 
    } 
) 
} 
... 

編輯

與無限監視循環觸發任務,在必要的時候:

... 
void Email(string message){} 

void Log(string message){} 

void CallWebService(string message){} 


void Monitor() 
{ 
    while(true) 
    { 
      string message = Monitor(); 
      if(ShouldNotify(message)) 
      { 
       var task = Task.TaskFactory.StartNew(() => 
       { 
        Email(mesasge); 
        Log(message); 
        CallWebService(message); 
       } 
     } 
    } 
) 
} 
... 

至於如何實現這些類,我會推薦一種方法,其中每個接收器接受消息&然後卸載它到它自己的處理線程/任務,以避免阻止您的監控任務&舉起其他通知。

+0

在CallWebService(消息)執行後,這個任務不會停止嗎?在我的情況下,我不希望任務被阻止。它將永遠運行。和thanx的鏈接。我會讀它。 – Aamir 2013-05-09 16:53:58

+0

是的,這個任務會在之後停止。我編輯了我的答案以提供一個無限運行的版本。 – Chris 2013-05-09 17:39:17

+0

非常感謝您的解釋......我編輯了我的問題,包含了一些代碼片段,介紹了我在做什麼/如何做...這將使您清楚地瞭解我需要什麼。真的很抱歉沒有提供此之前......我應該有 – Aamir 2013-05-09 19:14:31

0

Progress類只是完成這項任務。這是一種允許長時間運行的進程通知某人(通常是調用者)該操作當前進度的方法。

Progress<string> progress = new Progress<string>(); 
progress.ProgressChanged += (s, data) => Console.WriteLine(data); 

for (int i = 0; i < 2; i++) 
    Task.Run(() => DoWork(progress)); 

public static void DoWork(IProgress<string> progress) 
{ 
    int i = 0; 
    while (true) 
    { 
     Thread.Sleep(500);//placeholder for real work 
     progress.Report(i++.ToString()); 
    } 
} 

如果您有不同類型的信息在不同的時間報告然後就IProgress情況下,通過在多個的輔助方法。 (或者,如果您在報告幾種類型數據的進度的同時將所有數據包含在複合對象中)。

另請注意,這是能夠處理您所說的需要的同步。創建時,Progress實例會在創建時捕獲SynchronizationContext.Current的值,並將進度更改事件的所有事件處理程序封送到該同步上下文中。因此,如果您的應用程序已經具有上下文(即來自桌面應用程序的UI上下文),那麼您可以免費獲得該上下文。如果你沒有(即它是一個控制檯應用程序),那麼你需要手動同步事件處理程序與說lock,或創建自己的SynchrnonizationContext設置爲當前上下文。

+0

感謝您的詳細示例。如果我一開始就肯定會用到你的方法。但是現在我已經投入了很多精力去寫一種不同的方法。我編輯了我的問題,爲您提供我正在尋找的東西。我真的很抱歉沒有提供這之前......但在這種情況下,你不會告訴我自動跳轉Progress ...這似乎很酷..你可以再次看到提供的代碼並相應地建議我嗎? – Aamir 2013-05-09 19:12:51

相關問題