private void Log(TextBlock block, string message, bool toFile = false) { message = $"{DateTime.Now} {message}{Environment.NewLine}"; if (block.CheckAccess()) { if (block.Text.Length > 2500) { block.Text = block.Text.Substring(500); } block.Text += message; } else { block.Dispatcher.InvokeAsync(() => { block.Text += $"async: {message}"; }); } if (toFile) { lock (_locker) { File.AppendAllText("log.txt", message, Encoding.UTF8); } } }
//Function to write text to text display safely private void WriteTextSafe(string text) { if (!Messages.CheckAccess()) { Messages.Dispatcher.Invoke(new SafeCallDelegate(WriteTextSafe), text); } else { Messages.Text += text; } }
internal static void SetTextoTextBlock(TextBlock t, string s) { if (t != null) { if (t.CheckAccess()) { t.Text = s; } else { t.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetTextoTextBlockHandler(SetTextoTextBlock), t, s); } } }
private void SetTextOnTextBlock(TextBlock toSet, string toPass) { if (toSet == null) { return; } if (toSet.CheckAccess()) { toSet.Text = toPass; } else { toSet.Dispatcher?.Invoke(() => toSet.Text = toPass); } }
/// <summary> /// Set TextBlock text /// </summary> /// <param name="textBlock">The textblock</param> /// <param name="text">Text string</param> /// <param name="waitUntilReturn">Wait until return</param> public static void SetText(this TextBlock textBlock, string text, bool waitUntilReturn = false) { Action append = () => textBlock.Text = text; if (textBlock.CheckAccess()) { append(); } else if (waitUntilReturn) { textBlock.Dispatcher.Invoke(append); } else { textBlock.Dispatcher.BeginInvoke(append); } }
private void Log(TextBlock block, string message, bool toFile = false) { message = $"{DateTime.Now} {message}{Environment.NewLine}"; if (block.CheckAccess()) { if (block.Text.Length > 2500) { block.Text = block.Text.Substring(500); } block.Text += message; } else { block.Dispatcher.InvokeAsync(() => { block.Text += $"async: {message}"; }); } if (toFile) { Log(message); } }