MailBee.NET Objects 4.0

Imap.BeginSelectFolder Method 

Begins an asynchronous request for selecting a folder in an account on an IMAP4 server.

public IAsyncResult BeginSelectFolder(
   string folderName,
   bool readOnly,
   AsyncCallback callback,
   object state
);

Parameters

folderName
The full name of the folder to be selected.
readOnly
Indicates if the folder should be selected in read-only mode.
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 select folder process.

Remarks

This method is an asynchronous version of SelectFolder (if readOnly is false) or ExamineFolder (if readOnly is true).

Exceptions

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

Example

This sample demonstrates asynchronous selecting a folder and use of a callback function in a console application.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;

class Sample
{
    // A callback function.
    private static void SelectFolderCallback(IAsyncResult result)
    {
        Imap imp = (Imap)result.AsyncState;
        imp.EndSelectFolder();
        Console.WriteLine("Overall size of all messages in the inbox is " +
            imp.GetFolderSize() + " bytes");
    }

    // The actual code.
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        imp.Connect("imap4.somehost.com");
        imp.Login("jdoe@somehost.com", "secret");

        // Initiate an asynchronous select folder attempt.
        IAsyncResult ar = imp.BeginSelectFolder("Inbox", false,
            new AsyncCallback(SelectFolderCallback), imp);

        // Simulate some lengthy work here. At the same time,
        // folder selection is executed on another thread.
        System.Threading.Thread.Sleep(3000);

        // If the folder selection attempt is still in progress,
        // then wait until it's finished.
        while (imp.IsBusy) ar.AsyncWaitHandle.WaitOne();

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail

Module Sample

    ' A callback function.
    Private Sub SelectFolderCallback(ByVal result As IAsyncResult)
        Dim imp As Imap = CType(result.AsyncState, Imap)
        imp.EndSelectFolder()
        Console.WriteLine("Overall size of all messages in the inbox is " & _
            imp.GetFolderSize() & " bytes")
    End Sub

    Sub Main()
        ' The actual code (put it into a method of your class)
        Dim imp As New Imap

        ' Connect to the server and log in the account.
        imp.Connect("imap4.company.com")
        imp.Login("jdoe@company.com", "secret")

        ' Initiate an asynchronous select folder attempt.
        Dim ar As IAsyncResult = imp.BeginSelectFolder("Inbox", False, _
            New AsyncCallback(AddressOf SelectFolderCallback), imp)

        ' Simulate some lengthy work here. At the same time,
        ' folder selection is executed on another thread.
        System.Threading.Thread.Sleep(3000)

        ' If the folder selection attempt is still in progress,
        ' then wait until it's finished.
        While imp.IsBusy
            ar.AsyncWaitHandle.WaitOne()
        End While

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | ExamineFolder | SelectFolder