AsyncWaitHandle 终止第 3 方 API 是否正确实施?

发布于 2024-07-27 05:11:25 字数 1123 浏览 4 评论 0原文

“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2024-08-03 05:11:25

据我所知 http://msdn.microsoft.com/en- us/library/kzy257t0.aspx,您应该对 WaitOne() 方法的返回值进行逻辑检查,并围绕

您正在运行 EndInvoke 的逻辑,无论是否发生超时,因此您'从 session.Identify 中得到相同的超时错误。

result.AsyncWaitHandle.WaitOne(timeout, false); // checks if theres is a timeout and returns true/false
identifyDelegate.EndInvoke(out iresult, result); //code to run if WaitOne returns true

您可能想要这样做:

if(result.AsyncWaitHandle.WaitOne(timeout))
{
  identifyDelegate.EndInvoke(out iresult, result);
}
else
{
  //timeout occurred
  //handle timeout
}

更新:

您可能还想查看这个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.

result.AsyncWaitHandle.WaitOne(timeout, false); // checks if theres is a timeout and returns true/false
identifyDelegate.EndInvoke(out iresult, result); //code to run if WaitOne returns true

You'd probably want to do this:

if(result.AsyncWaitHandle.WaitOne(timeout))
{
  identifyDelegate.EndInvoke(out iresult, result);
}
else
{
  //timeout occurred
  //handle timeout
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文