Application.Idle事件的意义 [英] Application.Idle event significance

查看:661
本文介绍了Application.Idle事件的意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道怎么样的 Application.Idle 事件是在应用程序完成其处理并即将进入空闲状态。



我读的地方,




如果您有之前必须执行的任务线程变成
闲着,他们重视这一事件




那么,这是否意味着线程变得面前的任务将被执行空闲时,或线程变为空闲后?



我在我的项目,如下所示的一些代码。

时在空闲时正在执行的数据库更新?

 私人无效Application_Idle(对象发件人,EventArgs五)
{
//更新资源管理器的菜单项
team.UpdateMenu();

//更新显示的工具栏。
team.UpdateToolBar();

//更新白水回收
SaveAll.Enabled = teaj.IsModified;

Up.Enabled = team.CanNavigateUp;
...


解决方案

首先,要明白该Application.Idle是的的有关闲置线程,但有关应用程序的UI线程上的消息处理。 (空闲线程从消息循环空闲不同)



您的WinForms应用程序是由消息循环,拉出来的消息队列的驱动。当这个队列被清空,消息循环进入安静状态下,有效地睡觉,直到出现在消息队列中的下一条消息。这有助于节省CPU处理资源(周期浪费了一个循环纺纱需要CPU时间从机器上运行的其他程序了,所以一切都感觉慢),也有助于降低功耗/延长笔记本电脑的电池寿命。



您的应用程序的消息循环通常耗尽的消息队列积压相当频繁 - 即使你输入到编辑框中击键之间。



Application.Idle活动已成为照顾应用杂务异步与应用程序的主操作方便的地方,没有卷入多线程。



菜单和按钮通常启用或禁用应用程序时进入闲置状态,例如,以满足他们相应的命令的状态。由于可见外观只需要在用户的时间要更新(用户不能辨别视觉状态改变之间的差精确地以相比几毫秒以后改变可视状态的内部状态的变化的时间),应用程序空闲事件是一个简单而有效的机会,打理家务等琐事。



您可以把代码在你的WinForms应用程序的Application.Idle检查数据库或网络资源。但是,你一定要小心,不要做任何事情,以很长一段时间,因为如果你阻止Application.Idle,你的整个应用程序的用户界面将冻结。 。使用异步调用,而不是阻塞调用



另外,请记住,速率的Application.Idle事件触发变化很大 - 它有可能根据被解雇数次第二次或可能不会触发几秒钟,这取决于用户和应用程序在做什么。如果要检查数据更新的定期,你应该使用一个计时器事件,而不是Application.Idle。如果你开始一个异步网络请求每次Application.Idle火灾,您可以洪水有很多每秒(冗余)请求的服务器。


What I know about the Application.Idle event is that the application is finishing its processing and is about to enter the idle state.

I read somewhere that

If you have tasks that you must perform before the thread becomes idle, attach them to this event

So does this mean the tasks will be performed before the thread becomes idle, or after the thread becomes idle?

I have a bit of code in my project, shown below. Is the database update being performed during idle time?

private void Application_Idle(object sender, EventArgs e)
{
    // Update the explorer's menuitems
    team.UpdateMenu();

    // Update display toolbars.
    team.UpdateToolBar();

    // Update SaveAll
    SaveAll.Enabled = teaj.IsModified;

    Up.Enabled = team.CanNavigateUp;
    ...

解决方案

First, understand that the Application.Idle is not about "thread idle" but about message processing on the application's UI thread. (Thread idle is different from message loop idle)

Your WinForms app is driven by a message loop that pulls messages out of a queue. When that queue is emptied, the message loop enters a quiet state, sleeping efficiently until the next message appears in the message queue. This helps conserve CPU processing resources (cycles wasted spinning in a loop takes CPU time away from other processes running on the machine, so everything feels slower) and also helps reduce power consumption / extend laptop battery life.

Your app's message loop typically exhausts the message queue backlog fairly frequently - even between keystrokes when you are typing into an edit box.

The Application.Idle event has become a convenient place to take care of application housekeeping chores asynchronously with the primary operations of the app, and without getting involved with multiple threads.

Menus and buttons are typically enabled or disabled to match their corresponding command states when the application goes idle, for example. Since the visible appearance only needs to be updated in user time (the user can't discern the difference between visual state changes exactly at the time of internal state changes compared to changing the visual state a few milliseconds later), the application idle event is a simple and effective opportunity to take care of such housekeeping chores.

You could put code in your Winforms app's Application.Idle to check a database or network resource. However, you must be careful to not do anything that takes "a long time" because if you block Application.Idle, your entire app UI will freeze. Use async calls instead of blocking calls.

Also, keep in mind that the rate at which the Application.Idle event fires is highly variable - it may be fired several times per second or may not fire for several seconds, depending on what the user and your application are doing. If you want to check for data updates on a regular schedule, you should use a timer event instead of Application.Idle. If you start an async network request every time Application.Idle fires, you could flood your server with lots of (redundant) requests per second.

这篇关于Application.Idle事件的意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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