SocketTools .NET Edition

ImapClient.Idle Method (IdleOptions, Int32)

Enables mailbox status monitoring for the client session.

[Visual Basic]
Overloads Public Function Idle( _
   ByVal options As IdleOptions, _
   ByVal timeout As Integer _
) As Boolean
[C#]
public bool Idle(
   IdleOptions options,
   int timeout
);

Parameters

options
One or more of the IdleOptions enumeration flags.
timeout
Specifies the timeout period in seconds to wait for a notification from the server. This parameter is only used when the ImapIdle.idleWait option has been specified.

Return Value

This method returns a Boolean value. If the method succeeds, the return value is true. If the method fails, the return value is false. To get extended error information, check the value of the LastError property.

Remarks

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 providing 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 one of two modes, based on the options specified by the caller. If the option idleNoWait 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 idleWait 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.

The OnUpdate event is invoked within the context of the worker thread that is monitoring the client session. Because of this, applications should not directly update the user interface from within the event handler. 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 directly from within the event handler. Instead, it should create a delegate and use the Control.Invoke method to marshal the call to the thread that owns the control's window handle. Failure to do this can cause the application to become unstable. For more information, refer to the Control.Invoke method in the .NET Framework documentation.

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.

See Also

ImapClient Class | SocketTools Namespace | ImapClient.Idle Overload List | OnUpdate