c# - Best way to handle cancelling webclient async downloads -
i hoping shed light on seemingly complex problem having async webclient downloads in silverlight. here details of problem-
downloading multi-page xml web request. moved webclient code helperclass track each individual request. helper class called syncjobsclass. source class @ (http://www.silvergeek.net/silverlight/running-async-tasks-synchronously-events-delegates/)
have download button start task-
void button1_click(object sender, routedeventargs e) { var req = new requestclass; req.getxmlpages(); }
here request class-
public class requestclass { readonly syncjobsclass _syncjobs = new syncjobsclass(); public void getxmlpages() { _syncjobs += new syncjobselementcompleted; _syncjobs += new sycjobsworkcompleted; var pages = 10 for(var i=0;i<10;i++) { //load each request list in _syncjobs class var url ="www.blahblahblah.com" + i; _syncjobs.downloadfiles(url); } //start download of each page in list _syncjobs.startdownload(); } }
so can see after step have queue of pages being downloaded asynchronously. pages completed pass them xml linq formatter bind resulting data viewmodel.
void syncjobselementcompleted(string result) { if(result == "error")return; //send data xml linq parser , add observable collection viewmodel. app.viewmodel.loaddata(result); }
the problem large downloads if create new instance of getxmlpages() pressing download button, previous download not completed yet , mixed results in viewmodel. tried changing button event -
void button1_click(object sender, routedeventargs e) { var req = new requestclass; //clear view model before download app.viewmodel.list.clear() // pages req.getxmlpages(); }
well not work since download still going in different thread..
tried boolen valid instance of requestclass, gets recreated on each button press..
how can prevent download being started before create new request? if need more details let me know.. appreciated.
i think boolean correct route go--i've run similar situation before , used boolean solve problem. try storing boolean in app class not being re-created every time create new requestclass
.
Comments
Post a Comment