c# - Increment ProgressBar within another class -


it's first question i'm asking here, please gentle me ;)

so i've got 2 winforms in c# application i'm writing @ moment (i'm quite new c#).

this window has button, saves photos usb device selected before in list box folder. after clicking on button main thread of course busy copying, decided create winform contains progressbar.

foreach completed copy, want increment progressbar accordingly.

so count number of copies have , give progressbar maximum. problem @ moment is, don't know how increment progressbar without getting thread unsafe exception.

here's progresswindow code:

public partial class progresswindow : form {     backgroundworker updateprogressbarthread = new backgroundworker();      private boolean _isthreadrunning = false;     public boolean isthreadrunning     {         { return _isthreadrunning; }         set { _isthreadrunning = value; }     }      private int _progressbarlength;     public int progressbarlength     {         { return _progressbarlength; }         set { _progressbarlength = value; }     }      private int progress = 1;      public progresswindow()     {         show();         initializecomponent();     }      private void startupdatethread(object sender, doworkeventargs e)     {         backgroundworker worker = sender backgroundworker;          // reports progress progresschangedevent function. (thread safe)      }      private void finishprogressthread(object sender, runworkercompletedeventargs e)     {         if (!_isthreadrunning)         {             messagebox.show("erfolgreich kopiert");             close();         }      }      private void progresschangedevent(object sender, progresschangedeventargs e)     {         this.copyprogressbar.value = e.progresspercentage;         this.progressstatus.text = e.progresspercentage.tostring();     }      public void callupdatethread()     {         updateprogressbarthread.workerreportsprogress = true;          updateprogressbarthread.dowork += new doworkeventhandler(startupdatethread);         updateprogressbarthread.progresschanged += new progresschangedeventhandler(progresschangedevent);         updateprogressbarthread.runworkercompleted += new runworkercompletedeventhandler(finishprogressthread);         updateprogressbarthread.runworkerasync();     }  } 

i want increment progressbar 1 after each succesful copy. how do main thread?

this function handles copy process

private void savefile(system.io.directoryinfo root) {     try     {         ienumerable<directoryinfo> directoriesnames = root.enumeratedirectories();          // new instance of thread progresswindow.         progresswindow progress = new progresswindow();         progress.callupdatethread();          foreach (directoryinfo element in directoriesnames)         {             // query subdirectories , count in configuration made settings.             if (!element.attributes.tostring().contains("system"))             {                 // insert configuration applied.                 string fileextension = null;                  if (properties.settings.default._configphoto)                 {                     fileextension = "*.jpg";                 }                  if (properties.settings.default._configworddocument)                 {                     fileextension = "*.odt";                 }                  fileinfo[] jpglist = element.getfiles(fileextension, searchoption.alldirectories);                  // set size of progress bar                 progress.progressbarlength = jpglist.count();                  // go through our results , save them our backup folder.                 foreach (fileinfo tmp in jpglist)                 {                     string sourcefilepath = tmp.fullname;                     string destfilepath = pathtobackup + "\\" + tmp.name;                     progress.isthreadrunning = true;                      try                     {                                                         system.io.file.copy(sourcefilepath, destfilepath, true);                     }                     catch (ioexception ioe)                     {                         messagebox.show(ioe.message);                     }                 }             }         }         // progress.isthreadrunning = false;     }     catch (unauthorizedaccessexception e)     {         messagebox.show(e.message);     } } 

it's pretty obvious have after function

system.io.file.copy(sourcefilepath, destfilepath, true); 

but how report progresswindow?

i hope explained enough, not sure if i'm missing important.

thanks in advance guys

here compact example of key components:

  • clicking button starts new thread worker
  • progress done file lengths, not number of files
  • begininvoke used update progress bar (avoid cross thread exception)

        progressbar pb = new progressbar() { minimum = 0, maximum = 100 };     button btn = new button();     btn.click += delegate {         thread t = new thread(() => {             directoryinfo dir = new directoryinfo("c:\\temp\\");             var files = dir.getfiles("*.txt");             long totallength = files.sum(f => f.length);             long length = 0;             foreach (var f in files) {                 length += f.length;                 int percent = (int) math.round(100.0 * length / totallength);                 pb.begininvoke((action) delegate {                     pb.value = percent;                 });                  file.copy(f.fullname, "...");             }         });         t.isbackground = true;         t.start();     }; 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -