how to use text based console progress bar in multithreading C# -


hi have program downloads + extracts files in parallel (in threads). console app,and want show progress bar each operation in each thread. eg:

file 1 [==========35% ] 35mb of 100mb downloaded

file 2 [====20% ] 20mb of 100mb downloaded

file1 downloaded,

file 1 [=============50% ] 50% extracted. , on.

note: able show console outputs code below, use progress bar in console app.

how can use solution proposed in https://gist.github.com/danielswolf/0ab6a96899cc5377bf54 in case ?

public static void downloadandgetfiles()     {         try         {             parallel.foreach(fileids, currentid =>              {                 int currentid = fileids.id                 clientfiledownload(currentid);             });         }          catch (exception e)         {          }     }  private static void clientfiledownload(int currentid) {     webclient client = new webclient();     client.downloadprogresschanged += new downloadprogresschangedeventhandler(client_downloadprogresschanged);     client.downloadfilecompleted += new asynccompletedeventhandler(client_downloadfilecompleted);     string downloadedfile = @"d:\tmp\";     client.downloadfileasync(new uri(currentid.url), downloadedfile); //some url     while (client.isbusy) { }     string temp = extractandrename(currentid); }  private static void client_downloadprogresschanged(object sender, downloadprogresschangedeventargs e) {    //prints: "downloaded 3mb of 61.46mb  (4%)"    console.writeline("downloaded "               + ((e.bytesreceived / 1024f) / 1024f).tostring("#0.##") + "mb"               + " of "                                + ((e.totalbytestoreceive / 1024f) / 1024f).tostring("#0.##") + "mb"               + "  (" + e.progresspercentage + "%)"); }  private static string extractandrename(int currentid) {         //using sevenzipextractor lib http://stackoverflow.com/questions/20898794/how-to-extract-files-in-my-archive-one-by-one-using-sevenzipsharp         sevenzipextractor extractor = new sevenzipextractor(@"d:\tmp\" + id.name);         extractor.extracting += extractor_extracting;         extractor.extractarchive(@"d:\tmp\" + extractname[0]);         return (@"d:\tmp\" + extractname[0]); }  public static void extractor_extracting(object sender, sevenzip.progresseventargs p) {               console.foregroundcolor = consolecolor.yellow;               console.write("\b\b{0}% extracted", p.percentdone);               console.resetcolor(); } 

  1. provide every thread variable y contains line number allowed write to.
  2. before thread wants update screen, create lock. console can used 1 thread @ time. otherwise results of several threads mix up.
  3. move cursor line specified y , update line.
  4. release lock.

an example:

static private readonly object _sync = new object();  private static void updateprogress(int y, string item, int progress, int total) {     int percentage = (int)100.0 * progress / total;     lock(_sync)     {         console.cursorleft = 0;         console.cursortop = y;         console.write(item + " [" + new string('=', percentage / 2) + "] " + percentage + "%");     } } 

you can call method method clientfiledownload, has modified bit:

private static void clientfiledownload(int currentid, int y) 

and should called when creating threads this:

int y = 0; parallel.foreach(fileids, currentid => {      int currentid = fileids.id      clientfiledownload(currentid, y);     interlocked.increment(ref y); }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -