c# - What Is the appropriate way to report progress to a WPF view from a TPL task? -
c# - What Is the appropriate way to report progress to a WPF view from a TPL task? -
i trying read big number of rows database in little "chunks" or pages , study progress user; i.e., if i'm loading 100 "chunks" study progress each chunk loaded.
i using tpl in c# 4.0 read these chunks database , hand off total result set task can utilize it. sense tpl gives me improve command on task cancellation , hand off backgroundworker, etc., there doesn't seem built in way study progress of task.
here solution i've implemented study progress wpf progress bar , want create sure appropriate , there's not improve approach should taking.
i started creating simple interface represent changing progress:
public interface inotifyprogresschanged { int maximum { get; set; } int progress { get; set; } bool isindeterminate { get; set; } }
these properties can bound progressbar in wpf view , interface implemented backing viewmodel responsible initiating loading of info , reporting overall progress (simplified example):
public class contactsviewmodel : inotifyprogresschanged { private icontactrepository repository; ... private void loadcontacts() { task.factory.startnew(() => this.contactrepository.loadwithprogress(this)) .continuewith(o => this.useresult(o)); } }
you'll notice i'm passing viewmodel repository method inotifyprogresschanged , want create sure i'm not doing wrong.
my thought process here in order study progress, method doing work (which repository method) needs access inotifyprogresschanged interface in order study progress updating view. here's quick @ repository method (shortened example):
public class contactrepository : icontactrepository { ... public ienumberable<contact> loadwithprogress(inotifyprogresschanged indicator) { var resultset = new list<contact>(); var query = ... // linq entities query // set maximum number of "pages" iterated indicator.maximum = this.getnumberofpages(query.count(), this.pagesize); (int = 0; < indicator.maximum; i++) { resultset.addrange(query.skip(i * this.pagesize).take(this.pagesize)); indicator.progress += 1; // each "chunk" loaded, progress updated } // finish list returned after "chunks" loaded homecoming resultset.asreadonly(); } }
and how repository reporting progress view through viewmodel. right approach? using tpl properly, violating major rules, etc.? solution working, progress beingness reported expected, want create sure i'm not setting myself failure.
the "prescribed" way of doing passing taskscheduler
instance tasksheduler::fromcurrentsynchronizationcontext
continuewith
want ensure executes on wpf dispatcher thread.
for example:
public void dosomelongrunningoperation() { // called wpf dispatcher thread task.factory.startnew(() => { // execute on thread pool thread }) .continuewith(t => { // execute on wpf dispatcher thread }, taskscheduler.fromcurrentsynchronizationcontext()); }
c# c#-4.0 progress-bar task-parallel-library
Comments
Post a Comment