private 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)); } }
protected virtual void IdlePause() { if (_IdleTask == null || !_Idling) { return; } CheckConnectionStatus(); SendCommand("DONE"); if (!_IdleTask.Wait(ServerTimeout)) { //Not responding Disconnect(); ImapClientException e = new ImapClientException("Lost communication to IMAP server, connection closed."); ImapClientExceptionEventArgs args = new ImapClientExceptionEventArgs(e); Task.Factory.StartNew(() => ImapException.Fire(this, args)); } _IdleTask.Dispose(); _IdleTask = null; }