Reputation: 11
I'm trying to update my WPF GUI
from another thread
. I've found some solutions with using the Dispatcher but it's still not working.
For example I'm using it in a catch block:
catch (Exception err){
Dispatcher.BeginInvoke((Action)(() => {
pConsole.AppendText(err.Message);
pConsole.ScrollToEnd();
}));
return;
}
pConsole
is a RickTextBox.
With the dispatcher there is an output at the pConsole
, but the output is:
The calling thread cannot access this object because a different thread owns it.
Any idea what's wrong?
Upvotes: 0
Views: 188
Reputation: 5523
For a change, try appending the stack trace and the message to your RTF box.
catch (Exception err){
Dispatcher.BeginInvoke((Action)(() => {
pConsole.AppendText(err.Message);
pConsole.AppendText(err.StackTrace);
pConsole.ScrollToEnd();
}));
return;
}
There is nothing wrong with the code you wrote, it is just displaying the error message which was generated in the try block itself.
Upvotes: 0
Reputation: 86
It doesn't look like there is a problem with the code you have posted, but since you write that the RichTextBox
actually receives the text
The calling thread cannot access this object because a different thread owns it.
it seems like your exception handling code is working as it should, but catching an exception coming from the code in your try block.
In other words, check that the cause of the InvalidOperationException isn't in the try block.
Upvotes: 1