maria

maria

  • NA
  • 2
  • 0

GUI updations on socket callback in silverlight

Aug 13 2009 2:59 AM

I have a silverlight application,that communicating and getting data through socket communication ( this is a requirement ) .At each Receive callback i want to get data and update in the GUI at the moment itself.In this case sending and receiving data asynchronously,but the delegate for GUI updation in action only after all receive callbacks.How can i execute GUI updations sequentially with each callback?

 

My scenario is like this.

 

SocketReceiveCallback(object sender, SocketAsyncEventArgs e )

{

// read data from e.Buffer.

if ( dataReceived == TotalDataSize ) // checks of the complete reception of one  image

{

// update the GUI using the received data.

                        System.Diagnostics.Debug.WriteLine( " received image " );

Dispatcher.BeginInvoke(new DisplayImageDelgate(DislayImageData), imageData); // imageData is a class containing the received data

                       

}

Else

{

// call socket.receiveAsync() to get next set of data.
}

}

 

 

  private void DisplayImageData (MyImageData data)

        {

            if (data.ImageStream != null)

            {

                              

                BitmapImage bmp = new BitmapImage();

                bmp.SetSource(new MemoryStream(data.ImageStream));

                myReceivedImageCtrl.Source = bmp; //GUI update should happen at this point.

                               System.Diagnostics.Debug.WriteLine( " Draw image " );

 

            }

           

        }

 

Please note that my application, there is continues async requests to server ( corresponding to user action ) and server responds with a stream of image data( eg:- 100 images. The application needs to update the GUI immediately after each image is received ( even while the server streams the data). In the above case although the SocketReceiveCallback is getting the data continuously from server, the GUI update happens only after all the data receive is completed( which can take a long time ).

It seems that although the DisplayImage function is invoked using BeginInvoke, the system actually executes the display function at a later stage, causing 'jumping' image displays

The debug trace obtained was:-

 

received image

received image

received image

received image

Draw image

Draw image

Draw image

 

 

Instead of the needed

 

received image

Draw image

received image

Draw image

received image

Draw image

 

 

What could be done?

Thanks