AsyncWaitHandle 终止第 3 方 API 是否正确实施?
“session.identify”是我调用但无权访问的第三方COM API。 它执行的服务器查询会以某种方式偶尔锁定(从而停止等待结果的主程序)。
我的尝试是将其包装在 AsyncDelegate 中,这样我就可以给它一个超时,并在超时后允许主程序继续进行(类似于 这个,只是有一个返回值)。 然而,它仍然锁定,超时没有影响。
我是否错误地使用了 AsyncHandle.WaitOne? API 中是否有某些东西可以阻止它被中止?
private delegate void AsyncIdentifyCaller(CoAudioIdSignature signature, uint numResults, uint serverFlags , out IIdentifyResult result);
private IIdentifyResult identifyAndWait(CoAudioIdSession session, CoAudioIdSignature signature, uint numResults, out IIdentifyResult iresult)
{
AsyncIdentifyCaller identifyDelegate = new AsyncIdentifyCaller(session.Identify);
IAsyncResult result = identifyDelegate.BeginInvoke(
signature,
numResults,
0,
out iresult,
null,
null);
// wait up to timeout [ms] and then continue without a proper result
int timeout = 30000;
result.AsyncWaitHandle.WaitOne(timeout, false);
identifyDelegate.EndInvoke(out iresult, result);
return iresult;
}
"session.identify" is a third party COM API that I call and have no access to. It performs a server query that, somehow, locks up occasionally (and thus halts the main program which is waiting for the result).
My attempt was to wrap it in an AsyncDelegate so I would be able to give it a timeout and after expiration of the timeout allow the main program to proceed (similar to this one, just with a return value). However, it still locks up without the timeout having an effect.
Am I using the AsyncHandle.WaitOne incorrectly? Could there be something in the API that prevents it from being aborted?
private delegate void AsyncIdentifyCaller(CoAudioIdSignature signature, uint numResults, uint serverFlags , out IIdentifyResult result);
private IIdentifyResult identifyAndWait(CoAudioIdSession session, CoAudioIdSignature signature, uint numResults, out IIdentifyResult iresult)
{
AsyncIdentifyCaller identifyDelegate = new AsyncIdentifyCaller(session.Identify);
IAsyncResult result = identifyDelegate.BeginInvoke(
signature,
numResults,
0,
out iresult,
null,
null);
// wait up to timeout [ms] and then continue without a proper result
int timeout = 30000;
result.AsyncWaitHandle.WaitOne(timeout, false);
identifyDelegate.EndInvoke(out iresult, result);
return iresult;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知 http://msdn.microsoft.com/en- us/library/kzy257t0.aspx,您应该对 WaitOne() 方法的返回值进行逻辑检查,并围绕
您正在运行 EndInvoke 的逻辑,无论是否发生超时,因此您'从 session.Identify 中得到相同的超时错误。
您可能想要这样做:
更新:
您可能还想查看这个SO线程。 该问题似乎与您的问题几乎相同。 此外,接受的答案还提供了一种可重用的方法来实施错误管理
From what I can understand from http://msdn.microsoft.com/en-us/library/kzy257t0.aspx, you should have a logical check on the return value of WaitOne() method and wrap your logic around that
You're running the EndInvoke regardless if a timeout ocurrs or not, thus you're getting the same timout errors from session.Identify.
You'd probably want to do this:
UPDATE:
You might also like to check out this SO thread. The problem seems close to identical to yours. Also the accepted answer supplies a re-usable way to implement error-management