ImapBeginSetMessageFlags Method |
Note: This API is now obsolete.
Namespace: MailBee.ImapMail
[ObsoleteAttribute("This method is obsolete in .NET 4.5+. Use SetMessageFlagsAsync instead.")] public IAsyncResult BeginSetMessageFlags( string messageIndexSet, bool indexIsUid, string flags, MessageFlagAction action, bool silentMode, AsyncCallback callback, Object state )
Exception | Condition |
---|---|
MailBeeInvalidStateException | There is already an operation in progress. |
This WinForms samples deletes the first message in the inbox folder of the mail account asynchronously (in a worker thread). The sample then expunges the deleted messages (the expunge is also performed in worker thread).
The sample also demonstrates how to wait for asynchronous operation completion without blocking user interface of the application (the user can click buttons, move windows, etc) during the asynchronous processing. However, the sample does not allow the user to click a button which starts the asynchronous operation if this operation is already in progress.
// 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. bool finished; bool started = false; // A callback function. Since it's called on Imap worker thread, // it cannot display any messages or otherwise access UI. // Any updates of UI must take place on message loop thread. // See the sample in BeginSearch() topic on how to update UI in // such scenario. private void SetMessageFlagsCallback(IAsyncResult result) { Imap imp = (Imap)result.AsyncState; // It's important to handle exceptions in worker threads. Otherwise, // the worker thread could silently die and the application would // have never known this had happened. try { imp.EndSetMessageFlags(); imp.Expunge(); } catch (MailBeeException e) { MessageBox.Show(e.Message); } finally { finished = true; } } // Add button1 on the form to make this sample working. private void button1_Click(object sender, System.EventArgs e) { if (started) { MessageBox.Show("The operation is already in progress"); } else { started = true; } finished = false; Imap imp = new Imap(); // Connect to the server, login and select inbox. imp.Connect("mail.company.com"); imp.Login("jdoe", "secret"); imp.SelectFolder("Inbox"); // Asynchronously mark the first message as deleted. Instead of // MessageFlagSet.SystemFlagsToString(SystemMessageFlags.Deleted), // we could simply use "\Deleted" (@"\Deleted" in C#). imp.BeginSetMessageFlags("1", false, MessageFlagSet.SystemFlagsToString(SystemMessageFlags.Deleted), MessageFlagAction.Add, true, new AsyncCallback(SetMessageFlagsCallback), imp); // Wait until completion via periodic polling // of the completion status. while (!finished) { System.Threading.Thread.Sleep(1); // It's not necessary to call DoEvents here since we do not // handle any MailBee events in this sample. However, if we did, // we would have needed this call to let the events be processed. // Alternatively, we could have set imp.RaiseEventsViaMessageLoop // to false and use imp.Wait() instead of Thread.Sleep() and // Application.DoEvents(). If using imp.Wait(), we would not need // to prevent this routine from being called again while it's // already being executed since imp.Wait() blocks UI and it would // be impossible to click a button until imp.Wait() completes. Application.DoEvents(); } // Disconnect from the server. imp.Disconnect(); started = false; }