c# - FTP upload not working when run async, but works when run sync -
i trying upload file via ftp, , want report progress user. following this suggestion, couldn't make work.
if call code synchronously, works fine...
ftpwebrequest request = (ftpwebrequest)webrequest.create("ftp://myftpserver.com/test.zip"); request.credentials = new networkcredential("uid", "pwd"); request.method = webrequestmethods.ftp.uploadfile; using (filestream inputstream = file.openread(@"d:\test.zip")) { using (stream outputstream = request.getrequeststream()) { byte[] buffer = new byte[64 * 64]; int totalreadbytescount = 0; int readbytescount; while ((readbytescount = inputstream.read(buffer, 0, buffer.length)) > 0) { outputstream.write(buffer, 0, readbytescount); totalreadbytescount += readbytescount; int progress = (int)(totalreadbytescount * 100.0 / inputstream.length); debug.writeline(" " + progress + "%"); } } }
...but if try , wrap code in backgroundworker, fails silently. have tried adding try/catch block around it, don't exception.
here bgw version of code...
backgroundworker bg = new backgroundworker { workerreportsprogress = true }; bg.dowork += (s, e) => { try { debug.writeline("dowork"); ftpwebrequest request = (ftpwebrequest)webrequest.create("ftp://myftpserver.com/test.zip"); debug.writeline("dowork - setting creds"); request.credentials = new networkcredential("uid", "pwd"); request.method = webrequestmethods.ftp.uploadfile; using (filestream inputstream = file.openread(@"d:\test.zip")) { using (stream outputstream = request.getrequeststream()) { byte[] buffer = new byte[64 * 64]; int totalreadbytescount = 0; int readbytescount; while ((readbytescount = inputstream.read(buffer, 0, buffer.length)) > 0) { debug.writeline(" dowork - inside"); outputstream.write(buffer, 0, readbytescount); totalreadbytescount += readbytescount; double progress = totalreadbytescount * 100.0 / inputstream.length; debug.writeline(" " + progress + "%"); bg.reportprogress((int)progress); } } } } catch (exception ex) { debug.writeline("exception: " + ex.message); } }; bg.progresschanged += (s, e) => { debug.writeline(e.progresspercentage + "%"); }; bg.runworkercompleted += (s, e) => { debug.writeline("done"); }; bg.runworkerasync(); }
i "dowork" line written output window, nothing else. if put breakpoint on line sets ftpwebrequest, execution ends after line, don't exception.
anyone ideas? i'm doing wrong. want upload async, , have progress indicator. best way it?
in case helps anyone, problem nothing upload code, fine. problem that, speed (or thought) developing bit of code in console application. main() method called async upload method, exited. reason problem main method wasn't waiting until async method had finished, execution terminated.
the quick , dirty way around problem add line...
thread.sleep(10000);
...after call async method. however, requires guessing how long async method need, and/or being conservative , having wait longer necessary.
a better approach, requires waiting long needed can seen in iwolber's answer in this thread.
Comments
Post a Comment