我有,我创建的长时间运行的任务列表,监视一些系统/网络资源的情况,然后发送电子邮件,登录到一个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