WebClient等待操作 - Windows Phone 7

时间:2012-03-16 11:56:32

标签: windows-phone-7 windows-phone-7.1

我已经读过通信是异步WP7,但有些情况并不缺乏这种类型的通信。

我正在使用webclient下载内容,并希望在收到此类内容后进入下一个操作。

这是怎么做到的?

我是这个平台上的菜鸟。

Cumpz

2 个答案:

答案 0 :(得分:1)

听起来你正在寻找一种同步方法。如果是这种情况,你可以这样做:

        AutoResetEvent waitHandle = new AutoResetEvent(false);
        WebRequest request = WebRequest.Create(url) as HttpWebRequest;
        IAsyncResult asyncResult = request.BeginGetResponse(ar => waitHandle.Set(), null);
        if (!waitHandle.WaitOne(30000))
        {
            throw new TimeoutException("Timed out");
        }
        using (HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse)
        {
          ...

答案 1 :(得分:0)

这里有一些代码可以帮助您开始使用WebClient类

创建WebClient

WebClient client = new WebClient())
                client.DownloadStringAsync(new Uri("http://www.google.com"));
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

现在,在下载字符串操作完成后执行某些操作

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string result = e.Result;
        // Do something with the string
        DoThingWithString(result)
    }