void OnMessageLogged(object sender, LogEventArgs e) { if (!CheckAccess()) { this.Dispatcher.BeginInvoke((Action)(() => LogMessage(e))); } else { LogMessage(e); } }
void LogMessage(LogEventArgs e) { int caret = this.CaretIndex; int selectionStart = this.SelectionStart; int selectionLength = this.SelectionLength; bool scroll = (caret == this.Text.Length && selectionLength == 0); var newText = e.Message; if (newText.Length > MaxBufferSize) { this.Text = newText.Substring(newText.Length - MaxBufferSize); scroll = true; } else if (newText.Length + this.Text.Length > MaxBufferSize) { int keep = Math.Min(MaxBufferSize - newText.Length, this.Text.Length); int lost = this.Text.Length - keep; this.Text = this.Text.Substring(lost) + newText; if (lost >= caret && lost >= selectionStart) { scroll = true; } else { if (selectionLength > 0) { selectionStart -= lost; } caret -= lost; } } else { this.Text = this.Text + newText; } if (scroll) { this.CaretIndex = this.Text.Length; this.ScrollToEnd(); } else { this.CaretIndex = caret; if (selectionLength > 0) { this.SelectionStart = selectionStart; this.SelectionLength = selectionLength; } } }