I'm trying to use the IMAP IDLE feature to detect new mail. When a new mail is detected, the IDLE should stop and I will download the new mail.

However, when I call EndCheckForUpdates from the Notification event handler, it's just hanging. See example code below:

The class have these members:

Imap client; IAsyncResult async = null;

The client is properly connected to the server, and the inbox is selected. Then I do:

client.Notification += new ImapNotificationEventHandler(client_Notification); async = client.BeginCheckForUpdates(15*60000, CallBack, null);

This method is being called when a new mail arrives:

void client_Notification(object sender, ImapNotificationEventArgs e) { client.Abort(); // Tried both with and without this one if (async != null) client.EndCheckForUpdates(async); }

The problem is that the EndCheckForUpdates method call doesn't finish. How do I solve this? I want to cancel the IDLE as soon as I get a mail notification.

asked 15 Jan, 21:17

PatrikE's gravatar image

PatrikE
273
accept rate: 0%

edited 15 Jan, 21:18


Please note the Notification event is blocking. It means the calling thread continues when the client_Notification method returns.

In your case you made a deadlock. EndCheckForUpdates cannot finish, because it is waiting for the Notification event to be finished which is waiting for the EndCheckForUpdates to be finished.

The correct usage is as follows:

client.Notification += new ImapNotificationEventHandler(client_Notification);
client.BeginCheckForUpdates(15*60000, CheckForUpdatesCallback, null);

void client_Notification(object sender, ImapNotificationEventArgs e)
{
    // abort IDLE state
    client.Abort();
}

void CheckForUpdatesCallback(IAsyncResult ar)
{
    // end the IDLE operation
    client.EndCheckForUpdates(ar);
}
link

answered 16 Jan, 13:59

Lukas%20Matyska's gravatar image

Lukas Matyska ♦♦
78217
accept rate: 27%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×73

Asked: 15 Jan, 21:17

Seen: 41 times

Last updated: 16 Jan, 13:59