2013-05-09 62 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

相关问题