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:

  1. use task parallel library execute whole getresult-procedure asynchronous main thread.
  2. in task use webclient synchronously , return read data.
  3. specify continuation action via http://msdn.microsoft.com/en-us/library/hh194793.aspx , pass taskscheduler instance of main thread synchronized.
  4. 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

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -