The Idle method enables mailbox status monitoring for the
client session, allowing the client to receive notifications from the
server whenever a new message arrives or a message is expunged from
the currently selected mailbox. This is typically used as an
alternative to the client periodically polling the server for status
information.
Many IMAP servers support the ability to asynchronously send
status updates to the client, rather than have the client
periodically poll the server. The client enables this feature by
calling the Idle method and implementing an event handler for
the OnUpdate event. Typically these events inform the client
that a new message has arrived or that a message has been expunged
from the mailbox.
The Idle method can operate in two modes, based on the
options specified by the caller. If the option imapIdleNoWait
is specified, the method begins monitoring the client session
asynchronously and returns control immediately to the caller. If the
server sends a update notification to the client, the OnUpdate
event will fire with information about the status change. If the
option imapIdleWait is specified, the method will block
waiting for the server to send a notification message to the client.
The method will return when either a message is received or the
timeout period is exceeded.
Sending an IMAP command to the server will cause the client to
stop monitoring the session for status changes. To explicitly stop
monitoring the session, use the Cancel method.
This method works by sending the IDLE command to the server and
starting a worker thread which monitors the connection and looks for
untagged responses issued by the server. Events will be generated for
EXISTS, EXPUNGE and RECENT messages. Note that some servers may
periodically send untagged OK messages to the client, indicating that
the connection is still active; these messages are explicitly
ignored.
An application should never make an assumption about how a
particular server may send update notifications to the client.
Servers can be configured to use different intervals at which
notifications are sent. For example, a server may send new message
notifications immediately, but may periodically notify the client
when a message has been expunged. Alternatively, a server may only
send notifications at fixed intervals, in which case the client would
not be notified of any new messages until the interval period is
reached. It is not possible for a client to know what a particular
server's update interval is. Applications that require that degree of
control should not use the Idle method and should poll the
server instead.