“定时器”是否可以?占用更多CPU 资源?
我使用过计时器,发现它在几秒或几毫秒内做出决定时非常有用。 现在我有强烈的感觉,定时器的连续运行逐渐增加了处理器周期的消耗。
我创建了一个应用程序 (C#) 并使用“计时器滴答”每 1000 毫秒(1 秒)执行“三个”指令,我注意到 5 分钟后应用程序消耗了 5% 的 CPU 功率,10 分钟后消耗了 10%。
如果此进度保持不变,那么如果我在后台运行应用程序 4-5 小时后会发生什么?
我应该避免过度使用计时器吗?
private void currentTime_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("HH:mm:ss tt");
label2.Text = dt.AddSeconds(i).ToString("HH:mm:ss");
i++;
}
I've used timer and I found it very helpful while taking decisions in seconds or milliseconds.
Now I have strong feelings that continuous running of timer gradually increases consumption of processor cycles.
I've created an application (C#) and used 'timer tick' to execute 'three' instructions per 1000 milliseconds (1 sec) and I noticed that after 5 minutes application was consuming 5% of CPU power and 10% after 10 minutes.
If this progress remains constant then what will happen after 4-5 hours if I run my application in background?
Should I avoid excessive use of timer?
private void currentTime_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("HH:mm:ss tt");
label2.Text = dt.AddSeconds(i).ToString("HH:mm:ss");
i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,原因不是计时器本身,而是它调用的任何指令。您是否在这些指令中创建对象,或者调用在单独线程中运行的内容?启动线程或分配资源,然后忘记关闭它们肯定会导致您所描述的行为。
It seems to me that not the timer itself is the cause, but whatever instructions are called by it. Do you create objects in those instructions of yours, or call something that runs in a separate thread? Starting threads or allocating resources, and forgetting to close them can certainly lead to the behavior you described.