DispatcherTimer 和高 CPU 负载

发布于 2024-09-06 15:43:47 字数 543 浏览 5 评论 0原文

嘿,我有一个 WPF 应用程序,并使用 DispatcherTimer 每分钟触发一个事件。 我运行我的应用程序,CPU 负载跳至 100%。我尝试在不使用计时器的情况下编译应用程序,并且 CPU 负载非常低,正如我预期的那样。

示例代码:

DispatcherTimer MainTimer = new DispatcherTimer();
MainTimer.Tick += new EventHandler(Core.Timers.MainTimer_Tick);
MainTimer.Interval = TimeSpan.FromSeconds(60);
MainTimer.Start();

public static void MainTimer_Tick(object sender, EventArgs e)
{
 // initialize new class, do something...
}

如果没有该代码,CPU 负载就会很低。 这应该是什么原因造成的呢?

更新 我可以使用其他计时器吗?准确性并不重要。

Hey, I have an WPF application and using DispatcherTimer to fire an event every minute.
I run my app and the cpu load jumps to 100%. I tried to compile an app without using a timer and cpu load was very low as I it expected to be.

Sample code:

DispatcherTimer MainTimer = new DispatcherTimer();
MainTimer.Tick += new EventHandler(Core.Timers.MainTimer_Tick);
MainTimer.Interval = TimeSpan.FromSeconds(60);
MainTimer.Start();

public static void MainTimer_Tick(object sender, EventArgs e)
{
 // initialize new class, do something...
}

Without that code cpu load is low.
What should cause this?

Update
Can I use som other timer? Accuracy isnt important.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2024-09-13 15:43:47

我使用 System.Timers.Timer 解决了这个问题。它的行为正确。

I solved this with using System.Timers.Timer. It's behaving correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文