为窗口服务的不同类型的任务创建完美的多线程模型
本文关键字:完美 创建 多线程 模型 任务 同类型 窗口 服务 | 更新日期: 2023-09-27 18:33:38
我正在编写一个窗口服务。我有一些任务要每天执行一次。一组任务应该在时间间隔后执行,例如 5 分钟。最后一组任务应该在时间间隔后执行,比如 72 小时。
同时,我有一些设置需要按一定的时间间隔刷新。
我发现正确设计它有点问题。这是我的设计。
public void StartController()
{
try
{
readSettings();
startDetectionAndPost(); //once a day
LogUtil.WriteLog(LogLevel.INFO, "starting bootstrap");
bootStrap(); //based on time interval elapsed
LogUtil.WriteLog(LogLevel.INFO, "starting heartbeat of agent");
startHeartBeat(); //after a time interval say 1 min
LogUtil.WriteLog(LogLevel.INFO, "start fetching ADSI info");
startInfoTimer();
while (true)
{
if (!IsRunning)
{
engine.StopExecution();
break;
}
//here got to change this as per threading model.
while (_threadMonitor.CurrentThreadCount >= MAX_JOB_COUNT)
{
if (!IsRunning)
{
engine.StopExecution();
LogUtil.WriteLog(LogLevel.INFO, "query execution stopped");
break;
}
//otherwise sleep
Thread.Sleep(100);
}
_executionThread = new Thread(new ThreadStart(engine.StartExecution));
_threadMonitor.IncrementThread();
_executionThread.Start();
//if user gone stop service it will timeout issue.
Thread.Sleep(new TimeSpan(0, 0, 1));//_agentCache.DomainSweepInterval));
}
}
catch (Exception ex)
{
LogUtil.WriteLog(LogLevel.ERROR, "could not process."+ex.Message,ex);
//log exception and what to do next.
}
}
我每天必须执行一次的任务,或者每分钟后心跳。我为此创建了计时器并启动了它,因为这些东西独立于其他东西。 但随后是主要的执行部分。我有两种类型的查询要执行。1(每间隔后说5分钟,其他间隔后每72小时。一次只能执行一次。2(我必须在给定的时间间隔内刷新我的设置,例如在5分钟后,我必须刷新其他两个设置,例如在30分钟后刷新一次,在60分钟后刷新其他设置。
现在我发现很难正确安排它,以便我的服务启动/停止不会受到影响,并且一切都应该根据可扩展性和性能进行完美设计?
请分享你的观点,我希望我的问题框架得当。
谢谢
首先,我同意任务计划程序有其问题。 我以前不得不写其中之一,但我从头开始,我会看看这个项目。
http://taskschedulerengine.codeplex.com/
这使您能够爬上其他开发人员的背,并且可以专注于实际的业务问题。此外,如果您找到想要添加的内容,可以回馈社区