private void Listen() { _isRunning = true; while (_isRunning) { //move message processing into different function string line = _connection.Read(); if (!_isRunning) { break; } if (!_connection.IsConnected()) { var sw = System.Diagnostics.Stopwatch.StartNew(); //TalkyLog.Debug($"Client|Disconnected|"); _connection.Connect(); TalkyLog.Debug($"Client|Connected|{sw.ElapsedMilliseconds}"); //Task.Delay(500); continue; } if (string.IsNullOrEmpty(line)) { continue; } if (line.StartsWith("M:")) { ChatMessage theMessage = new ChatMessage(line.Substring(2)); _messageLog.Invoke((MethodInvoker) delegate { foreach (string piece in theMessage.Pieces.Keys) { _messageLog.SelectionColor = theMessage.Pieces[piece]; _messageLog.AppendText(piece); } _messageLog.AppendText(Environment.NewLine); }); if (_connection.IsConnected()) { _connection.Send("S:ChannelClientList"); } } else if (line.StartsWith("S:ChannelList:")) { string[] channels = line.Substring(14).Split(';'); new ChannelList(channels, _connection).ShowDialog(); } else if (line.StartsWith("S:ChannelClientList:")) { string[] clients = line.Substring(20).Split(';'); _clientListComboBox.Invoke((MethodInvoker) delegate { _clientListComboBox.Items.Clear(); _clientListComboBox.Items.AddRange(clients); }); } else if (line.StartsWith("S:Client:")) { string[] data = line.Substring(9).Split(';'); string username = data[0]; string muted = data[1]; string channel = data[2]; _titleLabel.Invoke((MethodInvoker) delegate { _titleLabel.Text = $@"[{username}] {channel} on {_connection.Host}:{_connection.Port}"; }); Invoke((MethodInvoker) delegate { Text = $@"[{username}] {channel} on {_connection.Host}:{_connection.Port}"; }); } else if (line.StartsWith("S:Account:")) { string[] data = line.Substring(9).Split(';'); string accountId = data[0]; string username = data[1]; string role = data[2]; _accountLabel.Invoke((MethodInvoker) delegate { _accountLabel.Visible = true; }); _accountUsernameLabel.Invoke((MethodInvoker) delegate { _accountUsernameLabel.Visible = true; _accountUsernameLabel.Text = username; }); _accountRoleLabel.Invoke((MethodInvoker) delegate { _accountRoleLabel.Visible = true; _accountRoleLabel.Text = role; }); _connection.Send("S:ChannelClientList"); } } }