c# - Background worker to copy directory -


in windows form application, directories copied on user request. copying these directories running on ui thread result, not able see progress of process.

currently copy function trigger following code.

 private void button5_click(object sender, eventargs e)         {               string[] str = combobox2.text.split(' ');             string username = str[2].replace("[", "").replace("]", "");             label3.text = combobox3.text;             dialogresult result = messagebox.show("do want copy " + combobox3.text + " mozilla profile " + username + " selected servers?", "confirmation", messageboxbuttons.yesnocancel);             if (result == dialogresult.yes)             {                 if (mycheck.checked == true)                 {                     string path = getpath(combobox3.text);                     try                     {                               string source = path + "\\appdata";                         string dest = "\\\\192.168.1.40\\c$\\users\\" + username + "\\appdata\\local\\mozilla";                         copy(@source, @dest);                      }                  }              }         } 

copy functions has following code.

     public void copy(string sourcedirectory, string targetdirectory)                 {                      directoryinfo disource = new directoryinfo(sourcedirectory);                     directoryinfo ditarget = new directoryinfo(targetdirectory);                     //gets size of files present in source folder.                     getsize(disource, ditarget);                     maxbytes = maxbytes / 1024;                      progressbar1.maximum = maxbytes;                     copyall(disource, ditarget);                 }   public void copyall(directoryinfo source, directoryinfo target)             {                  if (directory.exists(target.fullname) == false)                 {                     directory.createdirectory(target.fullname);                 }                 foreach (fileinfo fi in source.getfiles())                 {                      fi.copyto(path.combine(target.tostring(), fi.name), true);                      total += (int)fi.length;                      copied += (int)fi.length;                     copied /= 1024;                     progressbar1.visible = true;                     progressbar1.step = copied;                      progressbar1.performstep();                     label14.visible = true;                     label14.text = (total / 1048576).tostring() + "mb of " + (maxbytes / 1024).tostring() + "mb copied";                        label14.refresh();                 }                  foreach (directoryinfo disourcesubdir in source.getdirectories())                 {                        directoryinfo nexttargetsubdir = target.createsubdirectory(disourcesubdir.name);                     copyall(disourcesubdir, nexttargetsubdir);                 }             }              public void getsize(directoryinfo source, directoryinfo target)             {                   if (directory.exists(target.fullname) == false)                 {                     directory.createdirectory(target.fullname);                 }                 foreach (fileinfo fi in source.getfiles())                 {                     maxbytes += (int)fi.length;//size of file                   }                 foreach (directoryinfo disourcesubdir in source.getdirectories())                 {                     directoryinfo nexttargetsubdir = target.createsubdirectory(disourcesubdir.name);                     getsize(disourcesubdir, nexttargetsubdir);                  }              } 

my code working not able see progress , not able see update in label.

can me run copy in new thread can see progress in progress bar.

here's simple example use background worker.

public partial class form1 : form {     public form1()     {         initializecomponent();         shown += new eventhandler(form1_shown);          // report progress background worker need set property         backgroundworker1.workerreportsprogress = true;         // event raised on worker thread when worker starts         backgroundworker1.dowork += new doworkeventhandler(backgroundworker1_dowork);         // event raised when call reportprogress         backgroundworker1.progresschanged += new progresschangedeventhandler(backgroundworker1_progresschanged);     }     void form1_shown(object sender, eventargs e)     {         // start background worker         backgroundworker1.runworkerasync();     }     // on worker thread our thing!     void backgroundworker1_dowork(object sender, doworkeventargs e)     {         // background task goes here         (int = 0; <= 100; i++)         {             // report progress 'ui' thread             backgroundworker1.reportprogress(i);             // simulate long task             system.threading.thread.sleep(100);         }     }     // on 'ui' thread can update progress bar     void backgroundworker1_progresschanged(object sender, progresschangedeventargs e)     {         // progress percentage property of e         progressbar1.value = e.progresspercentage;     } } 

you can copyall in do_work , report progress show progress on ui in progress bar.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -