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:
{
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:
0 comments:
Post a Comment