C# 如何使用c语言使用定时器运行不同的进程#

C# 如何使用c语言使用定时器运行不同的进程#,c#,timer,C#,Timer,根据下面的代码,进程每1分钟运行一次 public partial class EmailService : ServiceBase { private Timer timer = null; public EmailService() { InitializeComponent(); } protected override void OnStart(string[] args) { timer = new Ti

根据下面的代码,进程每1分钟运行一次

public partial class EmailService : ServiceBase
{
    private Timer timer = null;
    public EmailService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer = new Timer();
        this.timer.Interval = 60000;
        this.timer.Elapsed += new ElapsedEventHandler(this.timer_Tick);
        this.timer.Enabled = true;
        Library.WriteErrorLog("Notification Service started.");
    }
    private void timer_Tick(object sender, ElapsedEventArgs e)
    {
        try
        {
            NotificationManager.ProcessApprovalNotifications();
            NotificationManager.CreateRenewalNotifications();
            NotificationManager.ProcessRenewalNotifications();
        }
        catch (Exception ex)
        {
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.Source);
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.Message);
            Library.WriteErrorLog("FAC VMS Notification Service Error: " + ex.StackTrace);
        }
        Library.WriteErrorLog("FAC VMS Notification Service Run");
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        Library.WriteErrorLog("Notification Service stopped.");
    }
}
如何使流程按照下面的列表运行

  • 每天一次=NotificationManager.CreateRenewalNotifications()
  • 每1分钟=NotificationManager.ProcessApprovalNotifications()
  • 每1分钟=NotificationManager.ProcessRenewalNotifications()

您可以记住上次在类字段中调用
NotificationManager.CreateRenewalNotifications
的日期时间,并且仅在一天过去后再次调用:

private Timer timer = null;
private DateTime lastCalledCreateRenewalNotifications = DateTime.MinValue;

NotificationManager.ProcessApprovalNotifications();
if (DateTime.Now - lastCalledCreateRenewalNotifications >= TimeSpan.FromDays(1))
{
    NotificationManager.CreateRenewalNotifications();
    lastCalledCreateRenewalNotifications = DateTime.Now;
}
NotificationManager.ProcessRenewalNotifications();

您可能还记得上次在类字段中调用
NotificationManager.CreateRenewalNotifications
的日期时间,并且只有在一天之后才再次调用它:

private Timer timer = null;
private DateTime lastCalledCreateRenewalNotifications = DateTime.MinValue;

NotificationManager.ProcessApprovalNotifications();
if (DateTime.Now - lastCalledCreateRenewalNotifications >= TimeSpan.FromDays(1))
{
    NotificationManager.CreateRenewalNotifications();
    lastCalledCreateRenewalNotifications = DateTime.Now;
}
NotificationManager.ProcessRenewalNotifications();

我建议使用一个alreay做这类事情的库,也许是一个调度器,在dotnet中有很多 如果你想建立自己的网站,请看这篇文章

使用系统;
使用System.Collections.Generic;
使用系统线程;
公共类SchedulerService
{
私有静态SchedulerService_实例;
私有列表计时器=新列表();
专用SchedulerService(){}
公共静态SchedulerService实例=>\u实例??(\u实例=新SchedulerService());
公共无效调度任务(整数小时、整数分钟、双间隔小时、操作任务)
{
DateTime now=DateTime.now;
DateTime firstRun=新的日期时间(now.Year,now.Month,now.Day,hour,min,0,0);
如果(现在>首次运行)
{
firstRun=firstRun.AddDays(1);
}
TimeSpan timeToGo=首次运行-现在;
如果(多哥时间)
{
task.Invoke();
},null,timeToGo,TimeSpan.FromHours(intervalInHour));
定时器。添加(定时器);
}
}
使用制度;
公共静态类MyScheduler
{
公共静态无效间隔秒(整数小时、整数秒、双间隔、动作任务)
{
间隔=间隔/3600;
SchedulerService.Instance.ScheduleTask(小时、秒、间隔、任务);
}
公共静态无效间隔分钟(整数小时、整数分钟、双间隔、动作任务)
{
间隔=间隔/60;
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
公共静态无效间隔时间(整数小时、整数分钟、双间隔、动作任务)
{
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
公共静态无效间隔(整数小时、整数分钟、双间隔、动作任务)
{
间隔=间隔*24;
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
}

我建议使用一个库来完成这类工作,可能是一个调度器,在dotnet中有很多 如果你想建立自己的网站,请看这篇文章

使用系统;
使用System.Collections.Generic;
使用系统线程;
公共类SchedulerService
{
私有静态SchedulerService_实例;
私有列表计时器=新列表();
专用SchedulerService(){}
公共静态SchedulerService实例=>\u实例??(\u实例=新SchedulerService());
公共无效调度任务(整数小时、整数分钟、双间隔小时、操作任务)
{
DateTime now=DateTime.now;
DateTime firstRun=新的日期时间(now.Year,now.Month,now.Day,hour,min,0,0);
如果(现在>首次运行)
{
firstRun=firstRun.AddDays(1);
}
TimeSpan timeToGo=首次运行-现在;
如果(多哥时间)
{
task.Invoke();
},null,timeToGo,TimeSpan.FromHours(intervalInHour));
定时器。添加(定时器);
}
}
使用制度;
公共静态类MyScheduler
{
公共静态无效间隔秒(整数小时、整数秒、双间隔、动作任务)
{
间隔=间隔/3600;
SchedulerService.Instance.ScheduleTask(小时、秒、间隔、任务);
}
公共静态无效间隔分钟(整数小时、整数分钟、双间隔、动作任务)
{
间隔=间隔/60;
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
公共静态无效间隔时间(整数小时、整数分钟、双间隔、动作任务)
{
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
公共静态无效间隔(整数小时、整数分钟、双间隔、动作任务)
{
间隔=间隔*24;
SchedulerService.Instance.ScheduleTask(小时、分钟、间隔、任务);
}
}

就个人而言,我会在
定时器滴答声开始时停止定时器,并在
定时器滴答声结束时再次启动,只是为了安全,它不会“超越”自身。就个人而言,我会在
定时器滴答声开始时停止定时器,并在
定时器滴答声结束时再次启动定时器,为了安全起见,它不会“超越”自己。