multithreading - While downloading something at the same time on label it should write downloading c# -


hi ppl im trying make write downloading while im downloading file c# tried thread method still writing after download finishes codes below;

  public void yap(object o)     {        (o label).text ="downloading";      }      private void button1_click(object sender, eventargs e)     {         checkforillegalcrossthreadcalls = false;         parameterizedthreadstart p = new parameterizedthreadstart(yap);         thread t = new thread(p);         t.start(label2);         string yol = environment.currentdirectory;         ftpwebrequest ftp;         try         {              filestream sr = new filestream(yol + "\\list.gz", filemode.create);              ftp = (ftpwebrequest)ftpwebrequest.create   (new  uri("ftp://"+textbox1.text+"/" + "/usr/valapp/etc/list.gz"));              ftp.credentials = new networkcredential("username", "password");              ftp.method = webrequestmethods.ftp.downloadfile;              ftp.usebinary = true;              ftpwebresponse response = (ftpwebresponse)ftp.getresponse();              stream ftpstream = response.getresponsestream();              long cl = response.contentlength;              int buffersize = 2048;             int readcount;              byte[] buffer = new byte[buffersize];             readcount = ftpstream.read(buffer, 0, buffersize);             while (readcount > 0)             {                 sr.write(buffer, 0, readcount);                 readcount = ftpstream.read(buffer, 0, buffersize);             }             ftpstream.close();             sr.close();             response.close();             messagebox.show("file downloaded!");         }         catch (exception ex)         {             messagebox.show(ex.message, "error");         }     }  } }   

as told codes working want write download @ same time or first thank you.

for windows app, think best use backgroundworker , dispatcher.invoke() updating label's text.

private void button1_click(object sender, eventargs e) {       //update label text here..        backgroundworker _backgroundworker = new backgroundworker();       _backgroundworker.dowork += _backgroundworker_dowork;       _backgroundworker.runworkerasync(); }  void _backgroundworker_dowork(object sender, doworkeventargs e) {         dispatcher.invoke(system.windows.threading.dispatcherpriority.background,             new action(delegate()         {             //download code here..         })); } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -