How can I access my Mainwindow-controls from a thread?
-
Hi, I am currently writing a gui-application, which does some processing in a thread. I pass a reference to my mainwindow to the thread, in order to make the thread set some text inside the mainwindow. However, when I try to access any control in my mainwindow from my thread, I get: "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll" Simple question: what is the best way to access mainwindo-gui elements from a thread? This is what my code looks like:
public MainWindow()
{
InitializeComponent();
_ProcessHelperThread = new System.Threading.Thread(ProcessHelperThread);
_ProcessHelperThread.Start();
}void ProcessHelperThread() { Processes.Init(this); //this is a reference to my mainwindow //run the processes defined in Startup configuration Processes.Startup(); }
Inside the thread (i.e. in my Processes-object), my app crashes when I try something like this:
public int Init(MainWindow window) { window.textBlock1.Text = Waittext; //Exception }
What is a better way to do this?
-
Hi, I am currently writing a gui-application, which does some processing in a thread. I pass a reference to my mainwindow to the thread, in order to make the thread set some text inside the mainwindow. However, when I try to access any control in my mainwindow from my thread, I get: "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll" Simple question: what is the best way to access mainwindo-gui elements from a thread? This is what my code looks like:
public MainWindow()
{
InitializeComponent();
_ProcessHelperThread = new System.Threading.Thread(ProcessHelperThread);
_ProcessHelperThread.Start();
}void ProcessHelperThread() { Processes.Init(this); //this is a reference to my mainwindow //run the processes defined in Startup configuration Processes.Startup(); }
Inside the thread (i.e. in my Processes-object), my app crashes when I try something like this:
public int Init(MainWindow window) { window.textBlock1.Text = Waittext; //Exception }
What is a better way to do this?
I would have expected a cross thread exception rather than the one you got but the correct way to update gui elements from another thread is the call Invoke on the control. This is for WinForms not WPF.
public Form1()
{
InitializeComponent();Thread workerThread = new Thread(WorkerThread); workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}private void UpdateWindowText(Form1 form)
{
form.Invoke((Action) (() => form.Text = "test"));
} -
I would have expected a cross thread exception rather than the one you got but the correct way to update gui elements from another thread is the call Invoke on the control. This is for WinForms not WPF.
public Form1()
{
InitializeComponent();Thread workerThread = new Thread(WorkerThread); workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}private void UpdateWindowText(Form1 form)
{
form.Invoke((Action) (() => form.Text = "test"));
}As a follow up this is how it is done in WPF.
public MainWindow()
{
InitializeComponent();Thread workerThread = new Thread(WorkerThread); workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}private void UpdateWindowText(Window window)
{
window.Dispatcher.Invoke((Action) (() => window.Title = "test"));
} -
As a follow up this is how it is done in WPF.
public MainWindow()
{
InitializeComponent();Thread workerThread = new Thread(WorkerThread); workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}private void UpdateWindowText(Window window)
{
window.Dispatcher.Invoke((Action) (() => window.Title = "test"));
}