public virtual bool TryGetResponse(out string response) { var resp = response = null; ResponseTask = Task.Factory.StartNew(() => { resp = GetResponse(IdleTimeout + ServerTimeout * 3); }); try { if (ResponseTask.Wait(IdleTimeout)) { response = resp; ResponseTask.Dispose(); ResponseTask = null; return(true); } else { return(false); } } catch (AggregateException) { throw; } }
void WatchIdleQueue() { try { string last = null, resp; while (true) { if (!TryGetResponse(out resp)) { // Child task should still running on ReadByte here. // Need to send some data to get it to exit. SendCommand("DONE"); //_ResponseTask should pick up response and exit if (!ResponseTask.Wait(ServerTimeout)) { // Not responding Disconnect(); throw new ImapClientException("Lost communication to IMAP server, connection closed."); } ResponseTask.Dispose(); ResponseTask = null; IdleResumeCommand(); continue; } if (resp.Contains("OK IDLE"))//Server response after DONE { return; } var data = resp.Split(' '); if (data[0] == "*" && data.Length >= 3) { var args = new MessageEventArgs { Client = this, MessageCount = int.Parse(data[1]) }; if (data[2].Is("EXISTS") && !last.Is("EXPUNGE") && args.MessageCount > 0) { Task.Factory.StartNew(() => newMessage.Fire(this, args)); //Fire the event in a task } else if (data[2].Is("EXPUNGE")) { Task.Factory.StartNew(() => messageDeleted.Fire(this, args)); } last = data[2]; } } } catch (Exception e) { var args = new ImapClientExceptionEventArgs(e); Task.Factory.StartNew(() => ImapException.Fire(this, args)); } }