相关疑难解决方法(0)

使用定时器间隔与任务延迟的重复任务

我正在实施像方法这样的预定工作,并且已经缩小到方法的范围。一个实现定时器间隔,另一个基于任务延迟。

我也考虑过使用 Azure Timer 触发的 webjobs,但它们在多实例模式下不起作用。实际上,在多实例应用程序中,只有一个触发器在其中一个实例中被触发,而其他触发器被锁定,因此增加我的应用程序的实例计数并不会增加触发的事件数量。

方法A(定时器间隔)

using System;
using System.Threading.Tasks;
using System.Timers;

public class Example
{
    public static void Main()
    {
        var aTimer = new Timer();
        aTimer.Interval = 5000;
        aTimer.Elapsed += OnTimedEventA;
        aTimer.AutoReset = true;
        aTimer.Enabled = true;

        var bTimer = new System.Timers.Timer();
        bTimer.Interval = 2000;
        bTimer.Elapsed += OnTimedEventB;
        bTimer.AutoReset = true;
        bTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }
    private static void OnTimedEventA(Object source, System.Timers.ElapsedEventArgs e)
    {
        Task.Run(() => …
Run Code Online (Sandbox Code Playgroud)

c#

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1