Blazor服务器端计时器正在运行 [英] Blazor Server Side Timer is running amok

查看:194
本文介绍了Blazor服务器端计时器正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行此ServerSide Blazor,并且有:

I have this ServerSide Blazor running and I have:

listTimer = new System.Timers.Timer();
        listTimer.Interval = 1000;
        listTimer.Elapsed += async (s, e) =>
        {
            await Utils.LogToConsole(jsRuntime, DateTime.Now.ToString());
        }

    `

但是,当我运行此程序时,我在Chrome控制台中会收到大量日志,但我希望每1000毫秒只能看到一次日志.任何人都看到此问题并找到了解决方法.是的,我可以检查实际时间并进行比较,但我不喜欢它触发那样的疯狂.

But when I run this I get huge amounts of log in the Chrome console but I expect to see only logs each 1000 millis. Anyone seen this problem and found a workaround. Yes I could check the actual time and compare but I dont like it triggering like thats crazy.

推荐答案

...我在Chrome控制台中收到大量日志

... I get huge amounts of log in the Chrome console

只有当您创建并分配了多个定时器时,这种情况才会发生.

That can only happen when you create and assign the Timer more than once.

您没有发布该代码,但我想您需要这样的内容:

You didn't post that code but I guess you need something like:

if (listTimer == null)
{
    listTimer = new System.Timers.Timer();
    listTimer.Interval = 1000;
    listTimer.Elapsed += async (s, e) =>
    {
        await Utils.LogToConsole(jsRuntime, DateTime.Now.ToString());
    }
}

这篇关于Blazor服务器端计时器正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆