Application.Idle event significance

peter picture peter · Mar 24, 2011 · Viewed 15.1k times · Source

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;
    ...

Answer

dthorpe picture dthorpe · Mar 24, 2011

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.