MailBee.NET Objects 4.0

Imap.BeginClose Method 

Begins an asynchronous request for closing (unselecting) the currently selected folder and optional removing of messages marked as deleted from this folder.

public IAsyncResult BeginClose(
   bool expungeDeleted,
   AsyncCallback callback,
   object state
);

Parameters

expungeDeleted
Indicates if the messages marked as deleted must be expunged.
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 unselecting folder and expunging deleted messages process.

Remarks

This method is an asynchronous version of Close.

Exceptions

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

Example

This WinForms sample demonstrates asynchronous unselecting the Inbox folder and expunging of all deleted messages if any.

[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 imp = null;
bool finished = false;

// A callback function. Since it's called on Imap worker thread,
// it cannot update controls on the form or otherwise access UI.
// The only UI-related action permitted in worker thread is
// displaying a message box (MessageBox.Show).
// Any updates of UI must take place on message loop thread.
// See BeginSearch method example on how to update UI in callbacks.
private void CloseCallback(IAsyncResult result)
{
    imp.EndClose();
    finished = true;
}

// The actual code.
private void Form1_Load(object sender, System.EventArgs e)
{
    imp = new Imap();

    // Connect to the server, login and select inbox.
    imp.Connect("mail.company.com");
    imp.Login("jdoe@company.com", "secret");
    imp.SelectFolder("Inbox");

    // Start async close/expunge operation.
    imp.BeginClose(true, new AsyncCallback(CloseCallback), null);

    // Wait until it's finished.
    while (!finished)
    {
        // Process events on message loop thread.
        // Alternatively, we could set imp.RaiseEventsOnMessageLoop
        // to false and use imp.Wait method to perform waiting.
        System.Threading.Thread.Sleep(1);
        Application.DoEvents();
    }

    // Close the connection.
    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.
Dim imp As Imap = Nothing
Dim finished As Boolean = False

' A callback function. Since it's called on Imap worker thread,
' it cannot update controls on the form or otherwise access UI.
' The only UI-related action permitted in worker thread is
' displaying a message box (MessageBox.Show).
' Any updates of UI must take place on message loop thread.
' See BeginSearch method example on how to update UI in callbacks.
Private Sub CloseCallback(ByVal result As IAsyncResult)
    imp.EndClose()
    finished = True
End Sub

' The actual code.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim imp As New Imap

    ' Connect to the server, login and select inbox.
    imp.Connect("imap.domain.com")
    imp.Login("jdoe", "secret")
    imp.SelectFolder("Inbox")

    ' Start async close/expunge operation.
    imp.BeginClose(True, New AsyncCallback(AddressOf CloseCallback), Nothing)

    ' Wait until it's finished.
    While Not finished
        ' Process events on message loop thread.
        ' Alternatively, we could set imp.RaiseEventsOnMessageLoop
        ' to false and use imp.Wait method to perform waiting.
        System.Threading.Thread.Sleep(1)
        Application.DoEvents()
    End While

    ' Close the connection.
    imp.Disconnect()
End Sub

See Also

Imap Class | MailBee.ImapMail Namespace | Close