在C#WINFORM或者是ASP.NET的WEB应用程序中,根据各种定时任务的需求,比如:每天的数据统计,每小时刷新系统缓存等等,这个时候我们得应用到定时器这个东东。

.NET Framework有自带的timer,但这个类只能完成一些简单的定时操作,比如间隔多久执行什么操作。遇到一些复杂的定时任务,如从当前时间开始,多少时间后间隔重复执行,timer类处理起来就相对困难了。经过多番查找搜索,终于找到一下比较好用的任务定时器–FluentScheduler,你可以通过Nuget来引用,用程序包管理器执行如下命令即可安装:

 
Install-Package FluentScheduler

此组件可以在C#和ASP.NET程序中使用,使用方法很简单,官方有使用案例:

 using FluentScheduler;
 
Registry
{
)
{
// Schedule an ITask to run at an interval
;
 
// Schedule an ITask to run once, delayed by a specific time interval.
;
 
// Schedule a simple task to run at a specific time
;
 
// Schedule a more complex action to run immediately and on an monthly interval
>
{
;
;
;
;
 
//Schedule multiple tasks to be run in a single schedule
;
}
}
 

在ASP.NET程序的Global.asax文件中,首先初始化管理器,这样定时器就开启了。

protected void Application_Start()
{
TaskManager.Initialize(new MyRegistry());
}

更多的应用还需自已实际操作,在此就不一一列举了。

相关文章:

  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
猜你喜欢
  • 2021-07-22
  • 2021-07-08
  • 2021-08-11
  • 2022-02-05
  • 2022-01-18
相关资源
相似解决方案