从Begin方法返回的IAsyncResult为null?

时间:2013-09-10 16:28:22

标签: c# asp.net asynchronous

如果我收到一些错误,我需要返回null。但这会引发,

IAsyncResult returned from Begin method is null.

这是我的代码?

        try
        {
            var url = (string)state;
            var request = (HttpWebRequest)WebRequest.Create(url);
            return request.BeginGetResponse(cb, request);
        }
        catch (Exception ex)
        { 
        }
        return null;

如何返回空的IAsyncResult?

1 个答案:

答案 0 :(得分:2)

只需实现此界面即可。

public class NullAsyncResult : IAsyncResult
{
public object AsyncState
{
get { return null; }
}

public System.Threading.WaitHandle AsyncWaitHandle
{
get { return null; }
}

public bool CompletedSynchronously
{
get { return true; }
}

public bool IsCompleted
{
get { return true; }
}
}

然后在你的代码中:

try
{
var url = (string)state;
var request = (HttpWebRequest)WebRequest.Create(url);
return request.BeginGetResponse(cb, request);
}
catch (Exception ex)
{
}
return NullAsyncResult();