I am trying to update a UI element from a different thread but I am clearly missing something with regards to the new WPF calls.
In 3.5 winforms I did something like the following when updating my UI via a different thread:
public string SummaryTitle
{
get
{
if (IsHandleCreated && InvokeRequired)
{
IAsyncResult result = BeginInvoke(new Func<object, object[], object>(GetType().GetProperty("SummaryTitle").GetValue), this, null);
return EndInvoke(result).ToString();
}
return myTextBox.Text.Trim();
}
set
{
if (IsHandleCreated && InvokeRequired)
{
BeginInvoke(new Action<object, object, object[]>(GetType().GetProperty("SummaryTitle").SetValue), this, value, null);
}
else
{
myTextBox.Text = value;
}
}
}
Now with 4.0 WPF I am trying to simulate the above, but with the new Dispatcher (maybe there is a different/better way, besides dispatcher?). Here is the prototype code I am playing with now (I know there are nonthreading related issues still in it, please ignore that for now) and it is not working right:
public string SomeText
{
get {
if(!myTextBox.Dispatcher.CheckAccess())
{
object o = myTextBox.Dispatcher.Invoke(new Func<object, object[], object>(GetType().GetProperty("SomeText").GetValue), this, null);
return o != null ? Convert.String(o) : "";
}
return myTextBox.Text.Trim();
}
set
{
if (!myTextBox.Dispatcher.CheckAccess())
{
myTextBox.Dispatcher.Invoke(new Action<object, object, object[]>(GetType().GetProperty("SomeText").SetValue),this, value, null);
return;
}
myTextBox.Text = value;
}
}
Any help on how to make the WPF accessor function similar to the old 3.5 winforms method would be appreciated.
EDIT: I am looking more at a way to make an accessor on the UI for a screen element thread-safe than the thread caller or progressbar specifically. The accessor needs to be able to be executed from another assembly or class regardless of whether that calling class is using Thread or BackgroundWorker. Also, it needs to hold water for all UI elements, not just a progress bar. So that a textbox, etc. can also be make thread safe in this manner. I updated the examples to help better convey my troubles.