This is a mirror of official site: http://jasper-net.blogspot.com/

Synchronous & Asynchronous Operations on Silverlight

| Sunday, July 31, 2011
A few days ago I wrote a very simple Silverlight application. Unfortunately I ended up with an infinitely locked UI thread.

Basically, the application was supposed to fetch configuration data from my server and use it. I added a reference to my server side service, and of course, Visual Studio generated an asynchronous proxy for me.

For a reason that is irrelevant for this post, I decided to wrap the asynchronous server call with a synchronous helper class. My helper class exposed a synchronous method which his parameters are the same as the server method except for the callback parameter that is not needed.

“One code of block worth a thousand words”, that’s the saying, right? :)

Let’s have a look at my helper class:

    class SynchronousConfigReader
    {
        private AutoResetEvent m_callbackCompleted;
        private string m_configValue;

        public SynchronousConfigReader()
        {
            m_callbackCompleted = new AutoResetEvent(false);
        }

        public string GetConfigData(string configKey)
        {
            ConfigServiceProxy proxy = new ConfigServiceProxy();
            proxy.GetConfigData(configKey, GetConfigData_Completed);
            m_callbackCompleted.WaitOne();
            return m_configValue;
        }

        private void GetConfigData_Completed(object sender, GetConfigDataEventArgs e)
        {
            m_configValue = e.ConfigValue;
            m_callbackCompleted.Set();
        }
    }


Read more: Shimi Rokah
QR: synchronous-amp-asynchronous-operations-on-silverlight.aspx

Posted via email from Jasper-net

0 comments: