vb.net - Backgroundworker Progressbar freezes -
so i've got almost work every few times test process form , progressbar freeze. i'm sure there more efficient ways of doing constructive criticism appreciated.
this coding 1 page of program allows user click 1 button download , install 1 application , press next button download , install different application:
imports system.net.webrequestmethods public class software 'open link in external browser public sub handlerequestnavigate(byval sender object, byval e requestnavigateeventargs) process.start(new processstartinfo(e.uri.absoluteuri)) e.handled = true end sub 'declarations shared progressamc new progress shared progresscti new progresscti withevents startcti new process withevents startamc new process withevents startsfstb new process withevents amcworker new componentmodel.backgroundworker withevents ctiworker new componentmodel.backgroundworker dim progressbaramc object = progress.progressbar1 dim blprgrsamc object = progress.blprgrs dim progressbarcti object = progresscti.progressbar1 dim blprgrscti object = progresscti.blprgrs 'ftp values const host string = "ftp://10.167.16.80/" const username string = "anonymous" const password string = "" 'amc file put/get const localfileamc string = "c:\amc.exe" const remotefileamc string = "bin/amc.exe" 'cti file put/get const localfilecti string = "c:\cti.exe" const remotefilecti string = "bin/cti.exe" 'on init public sub new() initializecomponent() amcworker.workerreportsprogress = true amcworker.workersupportscancellation = true ctiworker.workerreportsprogress = true ctiworker.workersupportscancellation = true end sub 'install amc button private sub buttonamc(sender object, e routedeventargs) dim butt1 button = directcast(sender, button) butt1.isenabled = false dispatcher.begininvoke(new action(addressof progressamc_show)) addhandler progress.cancel_click, addressof myprocessamc_exited amcworker.runworkerasync() end sub 'open dialog private sub progressamc_show() try progressamc.showdialog() catch ex exception messagebox.show("an error has occurred during process:" & vbcrlf & vbcrlf & ex.message & vbcrlf & vbcrlf & "please close application , try again." _ & vbcrlf & "if continue encounter error please email") end try end sub 'ftp - download private sub ftpseshamc_dowork(byval sender system.object, byval e componentmodel.doworkeventargs) handles amcworker.dowork dim uri string = host & remotefileamc dim ftp system.net.ftpwebrequest = ctype(system.net.ftpwebrequest.create(uri), system.net.ftpwebrequest) 'set credentials ftp.credentials = new system.net.networkcredential(username, password) 'ftp options ftp.keepalive = false ftp.usebinary = true 'define action download ftp.method = system.net.webrequestmethods.ftp.downloadfile 'get response ftp request , associated stream try dim response system.net.ftpwebresponse = ctype(ftp.getresponse, system.net.ftpwebresponse) dim length long = response.contentlength dim stopwatch new stopwatch dim currentspeed double = nothing using responsestream io.stream = response.getresponsestream 'loop read & write file using fs new io.filestream(localfileamc, io.filemode.create) dim buffer(2047) byte dim read integer = 0 dim count integer if amcworker.cancellationpending = true e.cancel = true return end if stopwatch.start() amcworker.reportprogress(cshort(count / length * 100 + 0.5)) read = responsestream.read(buffer, 0, buffer.length) fs.write(buffer, 0, read) count += read loop until read = 0 stopwatch.stop() responsestream.close() fs.flush() fs.close() end using responsestream.close() end using response.close() catch ex exception messagebox.show("an error has occurred during process:" & vbcrlf & vbcrlf & ex.message & vbcrlf & vbcrlf & "please close application , try again." _ & vbcrlf & "if continue encounter error please email") myprocessamc_exited() end try installamc() end sub 'starts installation sub installamc() startamc.startinfo.filename = "c:\amc.exe" startamc.enableraisingevents = true try startamc.start() catch ex exception msgbox(ex.message) end try dispatcher.invoke(new action(addressof progressamc_hide)) end sub 'hide dialog during install private sub progressamc_hide() progressamc.hide() end sub 'report progress private sub amcworker_progresschanged(byval sender system.object, byval e componentmodel.progresschangedeventargs) handles amcworker.progresschanged progressbaramc.value = e.progresspercentage blprgrsamc.content = "downloading: " & e.progresspercentage & "%" end sub end class again, appreciated.
edit: i've made following edit code i'm not entirely sure it's doing think it's doing. intended reportprogress run once every 2047 bytes read.
'get response ftp request , associated stream try dim response system.net.ftpwebresponse = ctype(ftp.getresponse, system.net.ftpwebresponse) dim length long = response.contentlength dim stopwatch new stopwatch dim currentspeed double = nothing using responsestream io.stream = response.getresponsestream 'loop read & write file using fs new io.filestream(localfileamc, io.filemode.create) dim buffer(2047) byte dim read integer = 0 dim count integer dim chunk integer = int(2047 / length) dim cycle integer = chunk = count if amcworker.cancellationpending = true e.cancel = true return end if stopwatch.start() if cycle = true amcworker.reportprogress(cshort(count / length * 100 + 0.5)) else end end if read = responsestream.read(buffer, 0, buffer.length) fs.write(buffer, 0, read) count += read loop until read = 0 stopwatch.stop() responsestream.close() fs.flush() fs.close() end using responsestream.close() end using response.close() catch ex exception messagebox.show("an error has occurred during process:" & vbcrlf & vbcrlf & ex.message & vbcrlf & vbcrlf & "please close application , try again." _ & vbcrlf & "if continue encounter error please email") myprocessamc_exited() end try
i didn't scrutinize code carefully, don't see why you're using stopwatch, took out references. i'm not sure starting multiple times inside loop , ending outside anyway.
the use of word end in second example comletely end app! pretty sure that's want there.
try modification of first code. key updating if change >= 5%:
using fs new io.filestream(localfileamc, io.filemode.create) dim buffer(2047) byte dim read integer = 0 dim count integer dim lastpct short = -5 dim pct short = 0 if amcworker.cancellationpending = true e.cancel = true return end if pct = cshort(count / length * 100 + 0.5) if pct>= (lastpct + 5) amcworker.reportprogress(pct) lastpct= pct endif read = responsestream.read(buffer, 0, buffer.length) fs.write(buffer, 0, read) count += read loop until read = 0 amcworker.reportprogress(100) responsestream.close() fs.flush() fs.close() end using
Comments
Post a Comment