MailBee.NET Objects 4.0

Imap.BeginIdle Method 

Begins an asynchronous request for starting idling mode.

public IAsyncResult BeginIdle(
   AsyncCallback callback,
   object state
);

Parameters

callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous idle process.

Remarks

This method is an asynchronous version of Idle. However, unlike Idle, using BeginIdle method usually does not require subscribing to Idling event because BeginIdle does not block and the application can use other techniques to keep UI responsive during idling.

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This WinForms sample is an asynchronous version of the sample listed in Idle method documentation. Idling event is not used but UI still does not freeze during idling.

[C#]
// To use the code below, import MailBee namespaces at the top of your code
using MailBee;
using MailBee.ImapMail;

// Put the code below inside your class.

// Imap object declared global because it's accessed from several methods.
Imap imp = null;

bool started = false;

// Monitor folder changes and save them in the log file.
private void imp_MessageStatus(object sender, ImapMessageStatusEventArgs e)
{
    ((Imap)sender).Log.WriteLine("Got " + e.StatusID + " status update");
}

// Start/stop idling.
// Add button1 on the form to make this sample working.
private void button1_Click(object sender, System.EventArgs e)
{
    if (started)
    {
        // Stop idling.
        imp.StopIdle();
        imp.EndIdle();

        // Disconnect from the server and revert to the original state.
        imp.Disconnect();
        button1.Text = "Go idle";
        started = false;
    }
    else
    {
        started = true;

        imp = new Imap();

        // Enable logging into a file.
        imp.Log.Filename = @"C:\log.txt";
        imp.Log.Enabled = true;
        imp.Log.Clear();

        // Connect to the server and check if IDLE is supported.
        imp.Connect("mail.domain.com");
        if (imp.GetExtension("IDLE") == null)
        {
            MessageBox.Show("IDLE not supported");
            imp.Disconnect();
            started = false;
        }
        else
        {
            // Login and select inbox.
            imp.Login("jdoe", "secret");
            imp.SelectFolder("Inbox");

            // Attach event handler.
            imp.MessageStatus +=new ImapMessageStatusEventHandler(imp_MessageStatus);

            button1.Text = "Stop idle";

            // Go idle in the background.
            imp.BeginIdle(null, null);
        }
    }
}

// Finish idling if the user closes the application.
// To make this method work, attach it to Closing event of the form.
private void Form1_Closing(object sender,
    System.ComponentModel.CancelEventArgs e)
{
    if (imp != null && imp.IsBusy)
    {
        imp.StopIdle();
        imp.EndIdle();
        imp.Disconnect();
    }
}
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code
Imports MailBee
Imports MailBee.ImapMail

' Put the code below inside your class.

' Imap object declared global because it's accessed from several methods.
Dim imp As Imap = Nothing

Dim started As Boolean = False

' Monitor folder changes and save them in the log file.
Private Sub imp_MessageStatus(ByVal sender As Object, ByVal e As ImapMessageStatusEventArgs)
    CType(sender, Imap).Log.WriteLine("Got " & e.StatusID & " status update")
End Sub

' Start/stop idling.
' Add button1 on the form to make this sample working.
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If started Then
        ' Stop idling.
        imp.StopIdle()
        imp.EndIdle()

        ' Disconnect from the server and revert to the original state.
        imp.Disconnect()
        button1.Text = "Go idle"
        started = False
    Else
        started = True

        imp = New Imap

        ' Enable logging into a file.
        imp.Log.Filename = "C:\log.txt"
        imp.Log.Enabled = True
        imp.Log.Clear()

        ' Connect to the server and check if IDLE is supported.
        imp.Connect("mail.domain.com")
        If imp.GetExtension("IDLE") Is Nothing Then
            MsgBox("IDLE not supported")
            imp.Disconnect()
            started = False
        Else
            ' Login and select inbox.
            imp.Login("jdoe", "secret")
            imp.SelectFolder("Inbox")

            ' Attach event handler.
            AddHandler imp.MessageStatus, AddressOf imp_MessageStatus

            button1.Text = "Stop idle"

            ' Go idle in the background.
            imp.BeginIdle(Nothing, Nothing)
        End If
    End If
End Sub

' Finish idling if the user closes the application.
' To make this method work, attach it to Closing event of the form.
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    If Not imp Is Nothing And imp.IsBusy Then
        imp.StopIdle()
        imp.EndIdle()
        imp.Disconnect()
    End If
End Sub

See Also

Imap Class | MailBee.ImapMail Namespace | Idle