Windows Mobile 6 UI更新问题
我有一个用 C# 编写的 Windows Mobile 应用程序,它有更多对话框。我想在触发事件时更新对话框。这是代码:
public void ServerStateChanged()
{
// update the interface
try
{
if (this.Focused)
{
this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
}
}
catch (Exception exc)
{
}
}
该代码运行了几次,但随后我通过以下堆栈跟踪得到了此 System.NotSupportedException
:at Microsoft.AGL.Common.MISC.HandleAr()\r\nat System.Windows.Forms.Control.get_Focused()\r\nat DialTester.Communication.TCPServerView.ServerStateChanged()\r\nat ...
从哪个线程触发该事件有关系吗?因为我不明白问题是什么,为什么它工作了几次然后就崩溃了。
I have a windows mobile application written in C# that has more dialogs. I want to update a dialog, when an event is triggered. Here is the code:
public void ServerStateChanged()
{
// update the interface
try
{
if (this.Focused)
{
this.noConnectionsLL.Text = this.tcpServer.ClientsCount.ToString();
}
}
catch (Exception exc)
{
}
}
The code works a few times, but then I get this System.NotSupportedException
with this stacktrace: at Microsoft.AGL.Common.MISC.HandleAr()\r\nat System.Windows.Forms.Control.get_Focused()\r\nat DialTester.Communication.TCPServerView.ServerStateChanged()\r\nat ...
Does it matter from which thread is triggered the event? because I can't figure out what the problem is, why it works a few times and then it crashes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者如下所示的兰巴方式。在我因使用 Control.BeginInvoke 而受到批评之前,BeginInvoke 是线程安全的并且完全异步(该调用会将更新放入 UI 事件队列中)。
Or the lamba way as below. And before I get criticised for using Control.BeginInvoke, BeginInvoke is threadsafe and is completely asynchronous (the call will put the update on the UI event queue).
这很可能是跨线程问题。在函数顶部检查 this.InvokeRequired 并做出相应反应肯定会提高函数的安全性。像这样的东西:
It's likely to be a cross-thread issue. Checking
this.InvokeRequired
at the top of the function and reacting accordingly would definitely improve the safety of the function. Something like this: