-
-
Notifications
You must be signed in to change notification settings - Fork 817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idle problen when fetch messages #749
Comments
You cannot query the ImapClient or an ImapFolder from within event callbacks. You have to just update your local state so that when the current command completes, you can then use that state info to decide what the next command(s) should be. The reason for this is that MailKit emits events immediately as they are sent across the wire from the IMAP server and does not wait until the current command has finished being processed. |
Is there any way to know when current command finish processing? |
Yea, when the method returns it means it's done processing. |
But I am thinking about event handler scenario.
|
This is what you can do: bool countChanged = false;
client.Inbox.CountChanged += (sender, args) => { countChanged = true; };
client.Inbox.DoSomething ();
if (countChanged) {
// now fetch the new messages
} |
Here's another option: Task task;
client.Inbox.CountChanged += (sender, args) => {
task = task.ContinueWith (...);
};
task = client.Inbox.DoSomethingAsync (); |
The problem was with coming back from idle when I received new message. Now I am doing sth like this -->
|
If you are just talking about breaking out of idle, you could do this: var done = new CancellationTokenSource (timeSpan);
client.Inbox.CountChanged += (sender, args) => { done.Cancel (); };
await client.IdleAsync (done.Token, cancel.Token); |
Most mail client software that supports IDLE uses multiple connections. They'll use 1 connection for IDLE and another connection to actually fetch the messages and such. That allows them to do something like this: idleClient.CountChanged += (sender, args) => { otherClient.Fetch (...); };
await idleClient.IdleAsync (doneToken, cancelToken); |
For future reference, see also this discussion: #1723 |
I am running the MailKit / samples / ImapIdle / project and I added a line of code in the CountChanged event to get the messages from the mail server in this way: var uids = folder.Search (SearchQuery.NotSeen); but when he executes that line he throws the next exception
The ImapClient is currently busy processing a command in another thread. Lock the SyncRoot property to properly synchronize your threads.
try lock SyncRoot in this way:
lock (folder.SyncRoot)
{
var uids = folder.Search (SearchQuery.NotSeen);
}
but keep throwing this exception. How can I solve that? Thanks in advance.
PS: I am using MailKit 2.0.1
The text was updated successfully, but these errors were encountered: