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;
     }
 }
Example #3
0
 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);
         }
     }
 }
Example #4
0
 private void SetTextOnTextBlock(TextBlock toSet, string toPass)
 {
     if (toSet == null)
     {
         return;
     }
     if (toSet.CheckAccess())
     {
         toSet.Text = toPass;
     }
     else
     {
         toSet.Dispatcher?.Invoke(() => toSet.Text = toPass);
     }
 }
Example #5
0
        /// <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);
            }
        }