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 Idle and providing the address of a callback function
that will be invoked whenever the server sends an update notification
to the client. Typically these updates 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 IMAP_IDLE_NOWAIT is
specified, the method begins monitoring the client session and
returns control immediately to the caller. If the server sends a
update notification, the callback function will be invoked with
information about the status change. If the option IMAP_IDLE_WAIT 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. To determine if
the current client session is being monitored, use the
GetIdleThreadId method. A non-zero return value indicates that
the client session is idle and being monitored.
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. Callbacks will be invoked
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. Because the monitoring is performed in a different thread,
the callback function is invoked in the context of that thread.
Client event notifications are disabled while inside the callback
function, and the Idle method cannot be used to restart
monitoring from within a callback function.
Applications should not perform any operation that takes a
significant amount of time or updates the user interface from within
the callback function. Instead, use flags or send application defined
messages to indicate a change in state. For example, if the server
sends a notification that a new email message has arrived, the
application should not attempt to read the new message and update the
user interface from within the callback function. Instead, it could
use the CWindow::PostMessage method to send an
application-defined message to the UI thread indicating the change in
state. The application would have a message handler for that Windows
message and update the user interface, indicating that a new message
has arrived.
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 Idle and should poll the server
instead.
BOOL CUserApp::MonitorUpdates()
{
INT nResult;
nResult = m_imapClient.Idle(UpdateHandler, (DWORD)this);
if (nResult == IMAP_ERROR)
{
m_imapClient.ShowError();
return FALSE;
}
return TRUE;
}
#define WM_APP_NEWMESSAGE (WM_APP+1)
#define WM_APP_EXPUNGED (WM_APP+2)
VOID CALLBACK CUserApp::UpdateHandler(HCLIENT hClient, UINT nUpdateId, UINT nMessageId, DWORD_PTR dwParam)
{
CUserApp *pThis = (CUserApp *)dwParam;
switch (nUpdateId)
{
case IMAP_UPDATE_MESSAGE:
{
pThis->m_pMainWnd->PostMessage(WM_APP_NEWMESSAGE, nMessageId, 0);
}
break;
case IMAP_UPDATE_EXPUNGE:
{
pThis->m_pMainWnd->PostMessage(WM_APP_EXPUNGED, nMessageId, 0);
}
break;
}
}