等待事件完成

6

我正在尝试将WebClient的响应转换为JSON,但它在从服务器下载完成之前就尝试创建JSON对象。 有没有什么“好”的方法可以让我等待WebOpenReadCompleted被执行?

需要提到这是一个WP7应用程序,所以一切都是异步执行的。

public class Client
{

    public String _url;
    private String _response;
    private WebClient _web;

    private JObject jsonsobject;
    private Boolean blockingCall;


    private Client(String url)
    {
        _web = new WebClient();
        _url = url;
    }

    public JObject Login(String username, String password)
    {
        String uriUsername = HttpUtility.UrlEncode(username);
        String uriPassword = HttpUtility.UrlEncode(password);

        Connect(_url + "/data.php?req=Login&username=" + uriUsername + "&password=" + uriPassword + "");
        jsonsobject = new JObject(_response); 
        return jsonsobject;
    }

    public JObject GetUserInfo()
    {

        Connect(_url + "/data.php?req=GetUserInfo");
        jsonsobject = new JObject(_response); 
        return jsonsobject;
    }

    public JObject Logout()
    {

        Connect(_url + "/data.php?req=Logout");
        jsonsobject = new JObject(_response); 
        return jsonsobject;
    }

    private void Connect(String url)
    {

        _web.Headers["Accept"] = "application/json";
        _web.OpenReadCompleted += new OpenReadCompletedEventHandler(WebOpenReadCompleted);
        _web.OpenReadAsync(new Uri(url));
    }

    private void WebOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error != null || e.Cancelled)
        {
            MessageBox.Show("Error:" + e.Error.Message);
            _response = "";
        } 
        else
        {
            using (var reader = new StreamReader(e.Result))
            {
                _response = reader.ReadToEnd();
            }    
        }
    }
}
2个回答

2

我看到你在使用OpenReadAsync()。这是一个异步方法,意味着在处理程序执行时,调用线程不会被挂起。

这意味着当WebOpenReadCompleted()仍在执行时,你的赋值操作设置jsonsobject就已经发生了。

我建议你最好将Connect(string url)方法中的OpenReadAsync(new Uri(url))替换为OpenRead(new Uri(url))。

OpenRead()是同步操作,所以在Connect()方法中进行赋值操作之前,调用方法将等待WebOpenReadCompleted()方法完成。


忘了提到它是一个WP7应用程序,因此WebClient没有同步操作,对此我感到抱歉。 - nickknissen
好的,那么,展示使用EventWaitHandle的另一篇文章是正确的方法。 - d3v1lman1337

2

您可以使用 EventWaitHandle 来优雅地阻塞,直到异步读取完成。我曾经有一个类似的需求,需要使用 WebClient 下载文件。我的解决方案是子类化 WebClient。完整的源代码如下。特别地,DownloadFileWithEvents 会在异步下载完成后优雅地阻塞。

对于您的目的来说,修改这个类应该很简单。

public class MyWebClient : WebClient, IDisposable
{
    public int Timeout { get; set; }
    public int TimeUntilFirstByte { get; set; }
    public int TimeBetweenProgressChanges { get; set; }

    public long PreviousBytesReceived { get; private set; }
    public long BytesNotNotified { get; private set; }

    public string Error { get; private set; }
    public bool HasError { get { return Error != null; } }

    private bool firstByteReceived = false;
    private bool success = true;
    private bool cancelDueToError = false;

    private EventWaitHandle asyncWait = new ManualResetEvent(false);
    private Timer abortTimer = null;

    const long ONE_MB = 1024 * 1024;

    public delegate void PerMbHandler(long totalMb);

    public event PerMbHandler NotifyMegabyteIncrement;

    public MyWebClient(int timeout = 60000, int timeUntilFirstByte = 30000, int timeBetweenProgressChanges = 15000)
    {
        this.Timeout = timeout;
        this.TimeUntilFirstByte = timeUntilFirstByte;
        this.TimeBetweenProgressChanges = timeBetweenProgressChanges;

        this.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(MyWebClient_DownloadFileCompleted);
        this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(MyWebClient_DownloadProgressChanged);

        abortTimer = new Timer(AbortDownload, null, TimeUntilFirstByte, System.Threading.Timeout.Infinite);
    }

    protected void OnNotifyMegabyteIncrement(long totalMb)
    {
        if (NotifyMegabyteIncrement != null) NotifyMegabyteIncrement(totalMb);
    }

    void AbortDownload(object state)
    {
        cancelDueToError = true;
        this.CancelAsync();
        success = false;
        Error = firstByteReceived ? "Download aborted due to >" + TimeBetweenProgressChanges + "ms between progress change updates." : "No data was received in " + TimeUntilFirstByte + "ms";
        asyncWait.Set();
    }

    void MyWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        if (cancelDueToError) return;

        long additionalBytesReceived = e.BytesReceived - PreviousBytesReceived;
        PreviousBytesReceived = e.BytesReceived;
        BytesNotNotified += additionalBytesReceived;

        if (BytesNotNotified > ONE_MB)
        {
            OnNotifyMegabyteIncrement(e.BytesReceived);
            BytesNotNotified = 0;
        }
        firstByteReceived = true;
        abortTimer.Change(TimeBetweenProgressChanges, System.Threading.Timeout.Infinite);
    }

    public bool DownloadFileWithEvents(string url, string outputPath)
    {
        asyncWait.Reset();
        Uri uri = new Uri(url);
        this.DownloadFileAsync(uri, outputPath);
        asyncWait.WaitOne();

        return success;
    }

    void MyWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (cancelDueToError) return;
        asyncWait.Set();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {            
        var result = base.GetWebRequest(address);
        result.Timeout = this.Timeout;
        return result;
    }

    void IDisposable.Dispose()
    {
        if (asyncWait != null) asyncWait.Dispose();
        if (abortTimer != null) abortTimer.Dispose();

        base.Dispose();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接