Starting Asynchronous Operations in Silverlight in Serial Mode

I understand the nature of Silverlight from Async, but one thing that constantly hinders my ability to program efficient interfaces is race conditions and reconnection problems caused by using objects that load data out of range. Is there a way to force operations out of range since they are in code?

For example, imagine that I have an object with a Load method whose only job is to load data from a stream into a text field. If I call it Async as such:

Object.Load( targetbox1 );
Object.Load( targetbox2 );
Object.Load( targetbox3 );

Target packages will load, but in any given order. If I reload the download code for any reason, then two loads will go through at the same time and are inserted into the same text field, leading to an exception. Most of these objects support events, and I could attach a callback as such:

Object.Loaded ( s, e ) => {

   Object.Load( targetbox2 );

    Object.Loaded( s, e ) => {
     ...
    }
}

Object.Load( targetbox1 );

But you see the problem with the attachment.

Is there any simple way to get these statements to run in a serial fashion so that one call is not executed until the previous one has completed, but in a simple, intuitive way that can be applied in all directions?

+3
source share
1 answer

. , - scenerio.

Load/Loaded: -

 AsyncOperation Load(YourObject subject, TextBox target)
 {
     return (completed) =>
     {
         EventHandler eh = null;
         eh = (s, args) =>
         {
             subject.Loaded -= eh;
             completed();
         }
         subject.Loaded += eh;
         subject.Load(target);
     }
 }

: -

 IEnumerable<AsyncOperation> LoadTextBoxes()
 {
      yield return Load(subject1, textbox1);
      yield return Load(subject2, textbox2);
      yield return Load(subject3, textbox3);
 }

: -

LoadTextBoxes().Run(e =>
{
   if (e != null)
   {
       //Something bad happened.
   }
});

, , , , , .

+3

All Articles