异步 Silverlight WCF 回调
我已经创建了自己的 WCF 服务,并且我已经成功地能够通过我的 Silverlight 客户端与其进行通信。不过,我在异步回调中遇到了一个有趣的问题。当我的回调被调用时,我无法使用可怕的无效的跨线程访问更新任何 UI 控件。
这是我的回调函数的样子。
private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
{
lblDisplay.Text = e.Object.ToString();
}
快速的 google 搜索告诉我,我必须这样做。
private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
{
Dispatcher.BeginInvoke( () => lblDisplay.Text = e.Object.ToString() );
}
现在一切正常,但我没想到我的回调会在不同的线程上运行。我是否总是必须使用 Dispatcher 类才能修改类中的任何内容,还是仅限于 UI 元素?我根本不熟悉 Dispatcher 类,所以我希望更多地了解它。
I've created my own WCF service and I've successfully been able to talk to it via my Silverlight client. I ran into an interesting problem on my asynchronous callbacks though. When my callback is invoked, I can't update any UI controls with the dreaded invalid cross thread access
Here's what my callback function looks like
private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
{
lblDisplay.Text = e.Object.ToString();
}
A quick google search showed me that I have to do this instead.
private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
{
Dispatcher.BeginInvoke( () => lblDisplay.Text = e.Object.ToString() );
}
Now everything works fine, but I wasn't expecting my callback to be running on a different thread. Will I always have to use the Dispatcher class in order to modify anything within my class or is this just limited to UI elements? I've not familiar with the Dispatcher class at all so I'm looking to understand it more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的..查看链接了解更多信息。我已添加乔尔对以下问题的答复
yes.. Checkout the link for more info. I have added Joel's reply to the question below