Application.Idle事件意义
我对 Application.Idle
事件的了解是应用程序正在完成其处理并即将进入空闲状态。
我在某处读到
如果您有必须在线程变为之前执行的任务 空闲,将它们附加到此事件
那么这是否意味着任务将在线程空闲之前或线程空闲之后执行?
我的项目中有一些代码,如下所示。数据库更新是否在空闲时间进行?
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;
...
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;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,了解 Application.Idle 与“线程空闲”无关,而是与应用程序 UI 线程上的消息处理有关。 (线程空闲与消息循环空闲不同)
您的 WinForms 应用程序由消息循环驱动,该消息循环从队列中提取消息。当该队列清空时,消息循环进入安静状态,有效地休眠,直到下一条消息出现在消息队列中。这有助于节省 CPU 处理资源(循环中浪费的旋转周期会占用计算机上运行的其他进程的 CPU 时间,因此一切都会变慢),并且还有助于降低功耗/延长笔记本电脑的电池寿命。
您的应用程序的消息循环通常会相当频繁地耗尽消息队列积压 - 即使在您在编辑框中键入内容时的两次击键之间也是如此。
Application.Idle 事件已成为一个方便的地方,可以与应用程序的主要操作异步处理应用程序内务杂务,并且无需涉及多个线程。
例如,当应用程序空闲时,通常启用或禁用菜单和按钮以匹配其相应的命令状态。由于可见外观只需要在用户时间内更新(与几毫秒后更改视觉状态相比,用户无法准确辨别内部状态更改时视觉状态更改之间的差异),因此应用程序空闲事件是一个简单而有效的机会来处理此类家务活。
您可以将代码放入 Winforms 应用程序的 Application.Idle 中来检查数据库或网络资源。但是,您必须小心,不要做任何需要“长时间”的事情,因为如果您阻止 Application.Idle,您的整个应用程序 UI 将冻结。使用异步调用而不是阻塞调用。
另外,请记住,Application.Idle 事件的触发速率变化很大 - 它可能每秒触发几次,也可能几秒钟内不触发,具体取决于用户和应用程序正在执行的操作。如果要定期检查数据更新,则应使用计时器事件而不是 Application.Idle。如果每次 Application.Idle 触发时都启动异步网络请求,则每秒可能会出现大量(冗余)请求淹没您的服务器。
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.