c# - Update textblock to value of variable in WPF -
edit: want update value of textblock value of random variable generated periodically on class.
my implementation blocking other features in app (buttons). suggestion?
public partial class mainwindow : window { taskviewmodel viewmodel = new taskviewmodel(); public mainwindow() { this.datacontext = viewmodel; initializecomponent(); server_v2.asyncservice.runmain(); displayav(); } //display availability private async void displayav() { while (true) { //availabilityfield.text = server_v2.av.tostring(); viewmodel.availability = server_v2.av.tostring(); await task.delay(500); } } public class taskviewmodel : inotifypropertychanged { private string availabilty = "0"; public string availability { { return availabilty; } set { availabilty = value; onstaticpropertychanged();} } public event propertychangedeventhandler propertychanged; public void onstaticpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
you should use background worker. async code still runs on main thread.
like this...
backgroundworker worker = new backgroundworker(); public mainwindow() { this.datacontext = viewmodel; initializecomponent(); server_v2.asyncservice.runmain(); worker.dowork += worker_dowork; worker.runworkerasync(); } private void worker_dowork(object sender, doworkeventargs e) { while (true) { //availabilityfield.text = server_v2.av.tostring(); viewmodel.availability = server_v2.av.tostring(); thread.sleep(500); } }
Comments
Post a Comment