Silverlight / WPF - can i make silverlight emulate synchronous call in silverlight class library?

Asked By Dan Hillman on 09-Apr-14 02:25 PM

I am trying to use your example of "makinghttp://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=13&cad=rja&uact=8&ved=0CDEQFjACOAo&url=http%3A%2F%2Fwww.nullskull.com%2Fa%2F1415%2Fmaking-silverlight-emulate-synchronous-requests.aspx&ei=R2BFU9CuBof7yAGG94HgDw&usg=AFQjCNEo5pKymRQIHKT0G_5Thu81P4a2JQ&bvm=bv.64507335,d.aWc" but for a Silverlight class library. I have something like this;


Thread thd = new Thread(new ParameterizedThreadStart(GetPage));

thd.IsBackground = true;

thd.Start("myurl");

string result = downloadStringResult;  (I set this public variable in the GetPage method)

I was thinking that after the thd.Start("myurl") statement, the next statement wouldn't be executed until after the GetPage was done.  However, that guess was incorrect.  Is it possible to do what I want, and if so, can you point me in the right direction?

thanks

Robbe Morris replied to Dan Hillman on 09-Apr-14 03:21 PM
This is problematic in Silverlight.  Add my voice to the chorus of people that will tell you that you need to bite the bullet and adapt to asynchronous calls.  That said, I know there are real world situations where you need to make these sorts of calls in a particular order before step b, step c, and step d can take place.  So, I'll spare you the arrogance you've no doubt gotten elsewhere.

What I've done in these situations is create a class that contains methods for each of these steps and subsequent events to fire when each step is complete and a final event to fire when all steps have been completed.  As each step's asynchronous handler raises its complete event, you fire off another event to trigger step B and so on.  The event handler for step d would fire the final event.  You'd wire up something in the UI to kill the app splash screen or some other UI characteristic telling the user your series of steps is done and the UI is ready to use.

I know it is a pain in contrast to synchronous calls that you are used to.  Silverlight uses the browser network stack and it is going to fight you and insist on performing network oriented tasks asynchronously whether you like it or not.