andyedinborough/aenetmail — Issue #6: Error: Cannot read when another read is pending
Author: NadavK
Created: 2011-11-28 15:09:19+00:00
State: open

Andy,

I am trying to use IDLE, but I receive an error in two scenarios: in the NewMessage delegate, and when calling .noop().
Can you help?

Error:
"The Read method cannot be called when another read operation is pending."

Call-stack:
at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
at AE.Net.Mail.TextClient.GetResponse() in C:\...\AE.Net.Mail\TextClient.cs:line 87
at AE.Net.Mail.TextClient.SendCommandGetResponse(String command) in C:\...\AE.Net.Mail\TextClient.cs:line 83
at AE.Net.Mail.ImapClient.IdlePause() in C:\...\AE.Net.Mail\ImapClient.cs:line 84
at AE.Net.Mail.ImapClient.GetMessages(String start, String end, Boolean uid, Boolean headersonly, Boolean setseen) in C:\...\AE.Net.Mail\ImapClient.cs:line 324
at AE.Net.Mail.ImapClient.GetMessages(Int32 startIndex, Int32 endIndex, Boolean headersonly, Boolean setseen) in C:\...\AE.Net.Mail\ImapClient.cs:line 319
at AE.Net.Mail.ImapClient.GetMessage(Int32 index, Boolean headersonly, Boolean setseen) in C:\...\AE.Net.Mail\ImapClient.cs:line 307
at AE.Net.Mail.ImapClient.GetMessage(Int32 index, Boolean headersonly) in C:\...\AE.Net.Mail\ImapClient.cs:line 303
at MailTester.Program.<>c__DisplayClass2.

b__0(Object sender, MessageEventArgs e) in C:\...\MailTester\Program.cs:line 35

Here is the code:

using (var imap = new ImapClient("imap.gmail.com", un, pw, ImapClient.AuthMethods.Login, 993, true)) {
var msgs = imap.SearchMessages(
SearchCondition.Undeleted().And(
SearchCondition.SentSince(new DateTime(2011, 10, 10))));
System.Diagnostics.Debug.WriteLine("msgs: " + msgs.Length.ToString());

imap.NewMessage += (sender, e) => {
var msg = imap.GetMessage(e.MessageCount - 1);
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToShortTimeString() + ": " + msg.Subject);
};

System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false);
while (!mre.WaitOne(TimeSpan.FromMinutes(20))) {
imap.Noop();
}
}