Reputation: 1375
I'm hoping to find a mechanism for accessing/manipulating a ui element (like textbox) properties in a separate Thread but accessing it using Dispatcher means giving control to UI thread which keeps the user from doing anything (which is something I don't want - to freeze ui..)
So I actually want to access the ui from a separate thread without needing to freeze the main UI thread.
Upvotes: 0
Views: 966
Reputation: 32202
You cannot access the dependency properties of UI elements on another thread. It is not allowed.
However updating a text property is blindingly fast and that is most likely not your bottleneck. Calculating what the value of that text property is may be slow. So for example if you have an event triggered from the UI you can spawn a task which will run on another thread and keep the UI responsive.
When the value is ready the code on the left hand side of the await will run on the UI thread and you can then modify any UI properties you wish.
SomeEvent += async (s,e) => {
var text = await Task.Run(()=>{
return SomeVeryExpensiveOperation();
});
MyTextBox.Text = text;
}
A more worked out example is here
http://www.codearsenal.net/2012/11/csharp-5-async-and-await-example.html#.WLhBlm_ysUE
Upvotes: 5
Reputation: 9827
In your case, you have to use static
class
with static events
, fire events from your Task
and handle the event in your ViewModel / Codebehind
, and do whatever you want in that event handler
.
Upvotes: -3