1

如果我得到一些错误,我需要返回 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?

4

1 回答 1

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();
于 2013-09-10T16:48:58.827 回答