First, understand that Application.Idle does not refer to the "idle stream", but about the processing of messages in the application user interface stream. (The idle thread is different from the idle message pipeline)
Your WinForms application is controlled by a message loop that outputs messages from the queue. When this queue is empty, the message loop goes into a calm state, effectively sinking until the next message appears in the message queue. This helps to save processor processing resources (cycles, wasted in the cycle, remove CPU time from other processes running on the machine, so everything becomes slower), and also helps to reduce power consumption / extend the battery life of the laptop.
Your application message loop usually quite often exhausts the backlog of the message queue - even between keystrokes when entering text in the edit field.
The Application.Idle event has become a convenient place for asynchronously monitoring the progress of work with the main operations of the application and without involving multiple threads.
Menus and buttons are usually turned on or off to match their respective command states, for example, when an application goes into standby mode. Since the visible appearance needs to be updated only at user time (the user cannot distinguish between visual state changes exactly during internal state changes compared to a visual state change a few milliseconds later), the application downtime event is a simple and effective opportunity to take care of such home operations .
You can put the code in a Winforms Application.Idle application to check the database or network resource. However, you must be careful not to do anything that takes a lot of time, because if you block Application.Idle, the entire user interface of the application will be frozen. Use asynchronous calls instead of blocking calls.
Also, keep in mind that the speed at which the Application.Idle event occurs is greatly changed - it may fire 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 regularly check for data updates, you should use a timer event instead of Application.Idle. If you run an asynchronous network request every time Application.Idle starts, you can flood your server with a lot of (redundant) requests per second.
dthorpe
source share