是否有某种方式让异步事件在Async中运行? oncomplete事件是从外部DLL调用的。外部DLL使用异步,我正在创建一个隐藏多线程的包装。在异步中运行oncomplete事件
我尝试过使用AutoResetEvents,但是这些卡在WaitOne上,因为重置AutoResetEvent的oncomplete事件只在函数完成后才运行。
从我可以从外部Dll拾取,对象树需要同步到主窗体才能正常工作。这会强制一个线程上的所有内容。 GetTag被称为GetTag(this)。
protected EventWaitHandle SyncEvent;
public string GetTag(object syncObject)
{
string lResult = "";
SyncEvent = new AutoResetEvent(false);
XiServer lOPCServer = new XiServer(OPCServer.ServerID, OPCServer.ApplicationName, syncObject); //create local instance of server synced to mainform
lOPCServer.Initiate(OnInitiated2, null); //Connect server
SyncEvent.WaitOne(); //Wait here for for connect to finish, Freezes here
if (ServerTree == null)
ServerTree = new ObjectTree(lOPCServer, lOPCServer.ServerInfo.ServerName);
DialogResult dlr = ServerTree.ShowDialog();
if (dlr == DialogResult.OK)
{
lResult = ServerTree.SelectedObject.InstanceId.FullyQualifiedId;
}
lOPCServer.Dispose();
return lResult;
}
private void OnInitiated2(Exception aError, object asyncState)
{
if (aError != null)
{
GlobalException = aError;
}
SyncEvent.Set();
}
所以总之,有没有办法让代码等待,直到Initiate被finsihed后?我没有访问启动代码。
您是否尝试创建自己的事件/委托? – RononDex
代表函数调用嵌入在dll中。你是否提出创建某种包装委托? – blackwolfsa
如果对LOPCServer.Initiate的调用没有阻塞,您可以将剩下的逻辑移到OnInitiated2回调(我猜)。 这就是Initiate调用下面的所有内容都转到了回调函数。 – matcheek