Question : C# Updating WPF with from Thread

Hi,

I am trying to figure out how to tell the main window thread to update the textbox from the thread I generated. I've read all over google about it and I just don't understand it. The examples vary a lot in structure so it's difficult to learn it when it's all different.

I get the following error when running this program:
/////////////////////////////////////////////////////////////////////////////////
System.InvalidOperationException was unhandled
  Message=The calling thread cannot access this object because a different thread owns it.
  Source=WindowsBase
  StackTrace:
       at System.Windows.Threading.Dispatcher.VerifyAccess()
       at System.Windows.Threading.Dispatcher.DisableProcessing()
       at System.Windows.Documents.TextContainer.BeginChange(Boolean undo)
       at System.Windows.Documents.TextContainer.System.Windows.Documents.ITextContainer.BeginChange()
       at System.Windows.Documents.TextRangeBase.BeginChangeWorker(ITextRange thisRange, String description)
       at System.Windows.Documents.TextRangeBase.SetText(ITextRange thisRange, String textData)
       at System.Windows.Documents.TextRange.System.Windows.Documents.ITextRange.set_Text(String value)
       at System.Windows.Controls.Primitives.TextBoxBase.AppendText(String textData)
       at WpfApplication7.MainWindow.appendToLog() in c:\users\user\documents\visual studio 2010\Projects\WpfApplication7\WpfApplication7\MainWindow.xaml.cs:line 41
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
/////////////////////////////////////////////////////////////////////////////////


Here is the program:
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace WpfApplication7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Thread t;
        private string myMessage;

        public MainWindow()
        {
            InitializeComponent();

            myMessage = "Thread me...\n";

            t = new Thread(appendToLog);
            t.Start();
        }

        private void appendToLog()
        {
            for (int i = 0; i < 10; i++)
            {
                tbLog.AppendText(myMessage);
                Thread.Sleep(1000);
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////////

Thank you for any help!

Answer : C# Updating WPF with from Thread

There are many ways to do it...here's one:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
    public partial class MainWindow : Window
    {

        private Thread t;
        private delegate void UpdateLog(string msg);

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            t = new Thread(appendToLog);
            t.IsBackground = true;
            t.Start();
        }

        private void appendToLog()
        {
            for (int i = 0; i < 10; i++)
            {
                Dispatcher.Invoke(new UpdateLog(UpdateLogMethod), new object[] { "Message #" + i.ToString() + Environment.NewLine});
                Thread.Sleep(1000);
            }
        }

        private void UpdateLogMethod(string msg)
        {
            tbLog.AppendText(msg);
        }

    }
Random Solutions  
 
programming4us programming4us