c# - Updating progressbar from a background thread -
i have progressbar , value binded property:
<progressbar x:name="progressbar" margin="0,2,0,0" height="20" value="{binding compasslogloadpercent}" foreground="blue" visibility="{binding compasslogloadcompleted, converter={staticresource booleantovisibilityconverter}}" tooltip="loading"> </progressbar>
and property:
public double compasslogloadpercent { { homecoming _compasslogloadpercent; } private set { if (value != _compasslogloadpercent) { _compasslogloadpercent = value; notifypropertychanged(); } } }
and in seperate thread value updated:
(int j = 0; j < lines.count(); j++) { ... compasslogloadpercent = ((double) j /lines.count())*100; }
and thread created using task:
task.run(() => { loadlogfile(filename); });
why progressbar not updating , how should prepare this?
update: more info
datacontext: (im sure datacontext correct)
clt.progressbar.datacontext = logsession;
and implementation of inotifypropertychanged
public event propertychangedeventhandler propertychanged; protected virtual void notifypropertychanged( [callermembername] string propertyname = "") { propertychangedeventhandler eventhandler = propertychanged; propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } }
the problem lies somewhere in haven't shown us. basic technique sound. (in particular, there's nil wrong raising propertychanged
event notifications on worker thread, because wpf's info binding scheme detects when happens, , automatically arranges update target ui element on ui thread.)
here's finish illustration work. here's xaml:
<window x:class="backgroundthreadupdate.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid> <progressbar x:name="progressbar" verticalalignment="top" height="20" value="{binding compasslogloadpercent}"> </progressbar> <button content="button" horizontalalignment="left" margin="10,25,0,0" verticalalignment="top" width="75" rendertransformorigin="-1.24,-0.045" click="button_click_1"/> </grid> </window>
and here's codebehind:
using system.componentmodel; using system.threading; using system.threading.tasks; using system.windows; namespace backgroundthreadupdate { public partial class mainwindow : window { private mysource _src; public mainwindow() { initializecomponent(); _src = new mysource(); datacontext = _src; } private void button_click_1(object sender, routedeventargs e) { task.run(() => { (int = 0; < 100; ++i) { thread.sleep(100); _src.compasslogloadpercent = i; } }); } } public class mysource : inotifypropertychanged { private double _compasslogloadpercent; public double compasslogloadpercent { { homecoming _compasslogloadpercent; } set { if (_compasslogloadpercent != value) { _compasslogloadpercent = value; onpropertychanged("compasslogloadpercent"); } } } public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } }
this illustrates working version of technique you're trying use.
so fact yours doesn't work must due you've not shown us. possible explanations:
your ui thread might blocked. if ui threads busy, info binding updates not processed. (when info binding detects alter info source on worker thread, posts message relevant dispatcher thread, , message won't processed if dispatcher thread busy.) the info source might not indatacontext
progressbar
- you've not shown set context, wrong. the code raises propertychanged
event (your notifypropertychanged
code) might wrong - you've not shown code, , it's not clear how knows property name utilize when raising event. to check first one, see if ui responsive user input while background work in progress. if it's not, that's why updates aren't getting through.
updated 25th feb add together relevant link
in thinking else how handle scenario, came conclusion big fit single stackoverflow answer. wrote series of blog posts performance considerations when doing non-trivial processing on background thread needs load info ui: http://www.interact-sw.co.uk/iangblog/2013/02/14/wpf-async-too-fast
c# wpf progress-bar
No comments:
Post a Comment