windows phone 7 - How to get main thread to wait on completion of StreamReader in a different class -
i developing app new wp7 development, have main process calling class uses streamreader read contents of webpage , assign variables. problem i'm having main process trying use variables before have been assigned values. there way make main process wait until streamreader has completed
in main thread have:
locationdetails = new locationresults(); locationdetails.getresults(addressdetails);
then in locationresults class
public void getresults(string address) { string addy, tmp; if (address[0] == '+') { tmp = address.substring(1); addy = baseaddress + tmp + "&sensor=false"; } else addy = baseaddress + address + "&sensor=false"; webclient webclient = new webclient(); webclient.openreadasync(new uri(addy)); webclient.openreadcompleted += new openreadcompletedeventhandler(webclient_openreadcompleted); } void webclient_openreadcompleted(object sender, openreadcompletedeventargs e) { string tmp; var reader = new streamreader(e.result); tmp = reader.readtoend().tostring(); results = tmp; }
it long time since worked wp 7 , .net think following approach help:
- use task parallel library execute whole getresult-procedure asynchronous main thread.
- in task use webclient synchronously , return read data.
- specify continuation action via http://msdn.microsoft.com/en-us/library/hh194793.aspx , pass taskscheduler instance of main thread synchronized.
- place code of main thread affects result in continuation action.
edit: guess code should that:
task.factory.startnew((adr)-> invokegetresult(adr string), addressdetails) .continuewith((data) -> handledatasynchronoustomainthread(data), taskscheduler.current);
but once again: has been long time since wrote last .net code ...
Comments
Post a Comment