multithreading - C# ProgressBar Databindings in backgroundworker -


i'm no expert in c# i'm trying update progressbar in background worker. i'm using following code:

progressbar1.databindings.add("value", _dm, "progress", true,                                datasourceupdatemode.onpropertychanged); 

this works when executed without background worker, on gui thread. progress property property updates (using inotifypropertychanged) progress of backgroundworker (where don't have access to).

how can make work updates use of backgroundworker instead of putting on gui thread?

my code (simplified):

class downloadmanager : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      private double _progressvalue;      public double progress     {         { return _progressvalue; }         private set         {             if (!value.equals(_progressvalue))             {                 _progressvalue = value;                 propertychanged?.invoke(this, new propertychangedeventargs("progress"));             }         }     }      public void download()     {         var downloader = new downloader();          downloader.downloadprogresschanged += (sender, e)                => progress = e.progresspercentage;          downloader.execute();     } }  public partial class mainform  {     private readonly downloadmanager _dm;     public mainform()      {         initializecomponent();         _dm = new downloadmanager();     }      private void btndownload_click(object sender, eventargs e)     {            //tried here ...            progressbar1.databindings.add("value", _dm, "progress", true,                                           datasourceupdatemode.onpropertychanged);             bwdownload.runworkerasync();         }     }      private void bwdownload_dowork(object sender, doworkeventargs e)     {         //and tried here          progressbar1.databindings.add("value", _dm, "progress", true, datasourceupdatemode.onpropertychanged);          //this aint working either         if (progressbar1.invokerequired) {             progressbar1.invoke(new methodinvoker(()                  => progressbar1.databindings.add("value", _dm, "progress", true,                                       datasourceupdatemode.onpropertychanged)));     }          _dm.download();     } } 

data bindings should created in constructor, need create binding once , not every time it's needed:

public mainform()  {     initializecomponent();     _dm = new downloadmanager();     progressbar1.databindings.add("value", _dm, "progress", true, datasourceupdatemode.onpropertychanged); } 

your code not working because try update ui non-ui thread. need need wrap code in invoke() call, example using proxy property on form:

public partial class mainform {        private double _progress;     public double progress     {         { return _progress; }         set         {             _progress = value;              // if not in ui thread -> wrap update in invoke() call:             if (this.invokerequired)             {                 this.invoke(new action(() => progressbar1.value = (int) _progress), new object[] { });                 return;             }              // else update directly             progressbar1.value = (int) value;         }     }      private readonly downloadmanager _dm;     public mainform()     {         initializecomponent();         _dm = new downloadmanager();          // bind _db.progress <-> this.progress         databindings.add("progress", _dm, "progress", true, datasourceupdatemode.onpropertychanged);     } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -