【发布时间】:2009-05-28 17:34:19
【问题描述】:
我有一个外部 COM 对象,它连接到服务器,然后在服务器准备好时触发一个事件。因此,connect() 调用是异步的。
我的代码看起来(有点……)像
ManualResetEvent waitConnection;
//This is the event that is triggered when server is ready
public void onConnection_event(bool success)
{
if(success)
waitConnection.Set();
}
private string getItem(string itemName)
{
//MYDBCom is a win32 com if that makes any difference
MYDBCom myDBobject = new MYDBCom();
waitConnection = new ManualResetEvent(false);
myDBobject.connect(); //asynchron call.
//wait until server triggers the onConnection_event
waitConnection.WaitOne();
//server is now ready. Get the data from the server.
return myDBobject.getItem(itemName);
}
问题是事件没有被触发 - 它似乎在 WaitOne 中等待时被阻止。如果我不使用 waitOne 使用
while(!connected)
{
Sleep(100);
DoEvents();
}
事件被触发。
任何想法为什么 WaitOne 阻塞?还有其他建议如何等待事件触发吗?
//弗雷德里克
【问题讨论】:
标签: c# multithreading events