locked
WPF textbox logging with multithread application RRS feed

  • Question

  • I'm new to C# and WPF so excuse me if this question is too basic.

    I have created a WPF application with a textbox where multiple threads are writing log text when calling the Log methode.

    The code seems to work but ends with an error everytime i put av break point in the code (VS). Why?

            private void Log(string text)
            {
                if (string.IsNullOrEmpty(text))
                    return;
    
                {
    
                }
                if (textBoxLog.Dispatcher.Thread == Thread.CurrentThread)
                {
                    textBoxLog.Text += text + Environment.NewLine;
                    textBoxLog.ScrollToEnd();
                }
                else
                {
                    this.Dispatcher.Invoke(() => textBoxLog.Text += text +Environment.NewLine);
                    this.Dispatcher.Invoke(() => textBoxLog.ScrollToEnd());
                }
            }



    Friday, November 9, 2018 5:15 PM

All replies

  • Hi Bjorn,

    I use multiple thread to call your method, but I don't get any error at my side, where you have error? Here is the my code, you can take a look:

                for(Int32 i=0;i<10;i++)
                {
                    //var t = Task.Run(() => Log("test" + i));
                    Thread t = new Thread(()=>Log("test"));
                    t.Start();
                }

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 12, 2018 3:36 AM