【问题标题】:C# Timeouts on Asynchronous callsC# 异步调用超时
【发布时间】:2012-04-10 10:12:14
【问题描述】:

在 C# 中进行异步调用后,我希望能够测试超时,因此我不会无限期地等待答案。这是我的代码:

RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.MethodHello);
// Call delegate to remote method
IAsyncResult RemAr = RemoteDel.BeginInvoke(null, null);
Console.WriteLine(RemoteDel.EndInvoke(RemAr));

有没有办法让EndInvoke 等待有限的时间(超时)?

如果没有,有没有其他方法?

【问题讨论】:

    标签: c# asynchronous timeout


    【解决方案1】:

    您可以尝试以下方法;

    if(RemAr.AsyncWaitHandle.WaitOne(1000)) // timeout value
    {
       Console.WriteLine(RemoteDel.EndInvoke(RemAr));
    }
    else
    {
       //throw exception etc.
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 1970-01-01
      • 2017-07-24
      • 2021-02-11
      • 2016-04-21
      • 1970-01-01
      • 2015-12-12
      • 2021-10-12
      • 1970-01-01
      相关资源
      最近更新 更多