c# - Get progress of an async call -


i'm using block of code got blog, upload images imgur using api v3.

it works fine, wanted implement progress bar system let user know how has been uploaded, if program deals high res images.

so far haven't been able so.

i'm not experienced coder, doing learning project.

the code:

public object uploadimage(string image)  {      webclient w = new webclient();     w.uploadprogresschanged += (s, e) => { };     w.uploadvaluescompleted += (s, e) => { };     w.headers.add("authorization", "client-id " + clientid);     system.collections.specialized.namevaluecollection keys = new system.collections.specialized.namevaluecollection();      try      {          keys.add("image", convert.tobase64string(file.readallbytes(image)));          byte[] responsearray = w.uploadvalues("https://api.imgur.com/3/image", keys);           dynamic result = encoding.ascii.getstring(responsearray); system.text.regularexpressions.regex reg = new system.text.regularexpressions.regex("link\":\"(.*?)\"");          system.text.regularexpressions.match match = reg.match(result);          string url = match.tostring().replace("link\":\"", "").replace("\"", "").replace("\\/", "/");         textbox1.text = url;         return url;      }     catch (exception s)      {          messagebox.show("something went wrong. " + s.message);              return "failed!";      }  } 

at first tried using events uploadprogresschanged , uploadvaluescompleted, not triggered, theory triggered when uploadvaluesasync called instead of uploadvalues.

  1. how implement progress system?
  2. what difference between async , normal transfer?

the difference between aync , normal transfer is, uploadvalues method block current thread until data has been transferred. because thread blocked in time can't catch events too. therefore have use asynchrony method uploadvaluesasync transfer data in background , you're able go on execution of code.
uploadprogresschanged fires uploadvaluesasync too. code should (not tested!):

public string uploadimage(string image)      {          webclient w = new webclient();          w.uploadprogresschanged += (s, e) =>         {             myprogressbar.maximum = (int)e.totalbytestosend;             myprogressbar.value = (int)e.bytessent;         };          w.uploadvaluescompleted += new uploadvaluescompletedeventhandler(uploadcomplete);          w.headers.add("authorization", "client-id " + clientid);         system.collections.specialized.namevaluecollection keys = new system.collections.specialized.namevaluecollection();          try          {              keys.add("image", convert.tobase64string(file.readallbytes(image)));              w.uploadvaluesasync("https://api.imgur.com/3/image", keys);              return "uploading..";         } catch (exception s)          {              messagebox.show("something went wrong. " + s.message);                  return "failed!";          }      }  public void uploadcomplete(object sender, uploadvaluescompletedeventargs e) {     myprogressbar.value = 100;      byte[] responsearray = e.result;     dynamic result = encoding.ascii.getstring(responsearray);      system.text.regularexpressions.regex reg = new system.text.regularexpressions.regex("link\":\"(.*?)\"");      system.text.regularexpressions.match match = reg.match(result);      string url = match.tostring().replace("link\":\"", "").replace("\"", "").replace("\\/", "/");     textbox1.text = url; } 

edit
moved code after uploadvaluesasync call w.uploadvaluescompleted. can find server response in result field of uploadvaluescompletedeventargs class passed event in variable e.
method uploadimage return uploading when progress started , you'll have rest work in w.uploadvaluescompleted event.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -