如果调用超时,我会继续进行并忘记我开始的线程调用。我的问题是,在超时情况下,我是否仍然需要以某种方式调用EndInvoke?这个MSDN页面上的“CAUTION”备注让我想知道是否应该:http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx 如果您认为我应该这样做,那么下一个问题是,如果我的网页在第三方返回之前完成处理并返回给客户端,回调方法是否仍会在那里监听运行代码?服务器是否在请求/响应完成后停止查找活动?
以下是我使用的代码:
public class RemotePaymentProcessor
{
private delegate string SendProcessPaymentDelegate(string creditCardNumber);
private string SendProcessPayment(string creditCardNumber)
{
string response = string.Empty;
// call web service
SlowResponseService.SlowResponseService srs = new WebServiceTimeout.SlowResponseService.SlowResponseService();
response = srs.GetSlowResponse(creditCardNumber);
return response;
}
public string ProcessPayment(string creditCardNumber, int timeoutMilliseconds)
{
string response = string.Empty;
SendProcessPaymentDelegate sppd = new SendProcessPaymentDelegate(SendProcessPayment);
IAsyncResult ar = sppd.BeginInvoke(creditCardNumber, null, new object());
if (!ar.AsyncWaitHandle.WaitOne(timeoutMilliseconds, false))
{
// Async call did not return before timeout
response = "TIMEOUT";
}
else
{
// Async call has returned - get response
response = sppd.EndInvoke(ar);
}
return response;
}
}