【发布时间】:2013-04-24 06:22:14
【问题描述】:
我正在处理一个软件的 GUI 线程。我必须在需要从底层 API 不断轮询的 GridView 数据中显示。我创建了一个名为Sync() 的方法来更新数据,并尝试使用BeginInvoke 和/或Invoke 使其线程安全。 Sync 方法由一个计时器每五秒左右调用一次。
Sync 的主体从未被调用,因为某些原因似乎从未从消息泵中提取我的请求。代码:
public void Sync()
{
if (this.InvokeRequired)
{
logger.Debug("Begin Invoke of Sync operation");
this.Invoke(new SyncDelegate(Sync));
logger.Debug("End Invoke of Sync operation");
}
else
{
logger.Debug("Inside body");
}
}
日志如下:
10:00:00 Begin Invoke of Sync operation
... other stuff ...
10:00:05 Begin Invoke of Sync operation
... other stuff ...
10:00:10 Begin Invoke of Sync operation
永远不会打印其他消息。
BeginInvoke 也一样。 “End Invoke ...”被打印,但“inside body ...”从不打印。
我在 Unity3D 和 MonoDevelop 上运行(如果这有什么改变的话)。
【问题讨论】:
标签: c# message-queue invoke invokerequired message-pump