Asynchronous operations are useful because they occur without waiting for the action to complete before executing the next line of code. Such a technique is useful when an operation is time consuming and other code needs to execute without waiting for the operation to complete.
Asynchronous methods are easily detectable in that they always begin with BeginXXX. This means the method will begin some operation on a new thread and immediately execute the next line of code. When the method has completed, the corresponding EndXXX event will be raised, notifying you that the operation has completed.
When creating an IMAP application, you will find many classes raise asynchronous methods (ex. BeginLogin - Imap class, MailBox.BeginCopy - Mailbox class, BeginGet - ImapMessageClass) however, the corresponding EndXXX event is always a member of the Imap component (for the previously stated examples, the EndLogin, EndCopy, EndGetMessage events of the Imap component). The following topic provides a walk-through of how to log into a server asynchronously using BeginLogin method.
To login to a server asynchronously.
- Add the Imap component to a new form.
- Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
C#
Copy Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- Within the button's onClick event, begin to asynchronously login to the server using the Imap.BeginLogin method.
C#
Copy Code// Login asynchronously. Program execution will continue without waiting for a response. imap1.BeginLogin("myserver", "user", "pass", null);
Visual Basic
Copy Code' Login asynchronously. Program execution will continue without waiting for a response. Imap1.BeginLogin("myserver", "user", "pass", Nothing)
- When the Imap.BeginLogin method has completed, the Imap.EndLogin event will be raised. "Wire-up" the Imap.EndLogin event. When complete, you should have an event handler method "wired-up" which looks like the following.
C#
Copy Codeprivate void imap1_EndLogin(object sender, Dart.PowerTCP.Mail.ImapResponseEventArgs e) { }
Visual Basic
Copy CodePrivate Sub Imap1_EndLogin(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.ImapResponseEventArgs) Handles Imap1.EndLogin End Sub
- When the EndLogin event is raised, take some action (in this example, the mailbox count is displayed). Your entire EndLogin event should look something like the following.
C#
Copy Codeprivate void imap1_EndLogin(object sender, Dart.PowerTCP.Mail.ImapResponseEventArgs e) { // Check for an exception if(e.Exception == null) { // Display some info Debug.WriteLine("Response from server: " + e.Response.ToString()); Debug.WriteLine("Top-level mailboxes found: " + imap1.Mailboxes.Count); } }
Visual Basic
Copy CodePrivate Sub Imap1_EndLogin(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.ImapResponseEventArgs) Handles Imap1.EndLogin ' Check for an exception If e.Exception Is Nothing Then ' Display some info Debug.WriteLine("Response from server: " + e.Response.ToString()) Debug.WriteLine("Top-level mailboxes found: " + imap1.Mailboxes.Count) End If End Sub