相关疑难解决方法(0)

为什么WebClient.DownloadStringTaskAsync()会阻塞? - 新的异步API /语法/ CTP

出于某种原因,在下面的程序开始后有一个暂停.我相信这WebClient().DownloadStringTaskAsync()就是原因.

class Program
{
    static void Main(string[] args)
    {
        AsyncReturnTask();

        for (int i = 0; i < 15; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(100);
        }
    }

    public static async void AsyncReturnTask()
    {
        var result = await DownloadAndReturnTaskStringAsync();
        Console.WriteLine(result);
    }

    private static async Task<string> DownloadAndReturnTaskStringAsync()
    {
        return await new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov"));
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我的程序应该立即从0到15开始计数.难道我做错了什么?

我在原始Netflix下载示例(使用CTP获得)时遇到了同样的问题- 按下搜索按钮后,UI首先冻结 - 一段时间后,它在加载下一部电影时响应.而且我认为它并没有冻结Anders Hejlsberg在PDC 2010上的演讲.

还有一件事.而不是

return await new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov"));
Run Code Online (Sandbox Code Playgroud)

我用自己的方法:

return await ReturnOrdinaryTask();
Run Code Online (Sandbox Code Playgroud)

这是:

public static Task<string> ReturnOrdinaryTask()
{
    var t = …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous async-await c#-5.0

15
推荐指数
2
解决办法
1万
查看次数

异步WebClient不是真正的异步?

我在类中创建了一个异步WebClient请求,如下所示:

public class Downstream
    {
        public bool StartDownstream()
        {
            WebClient client = new WebClient();

            client.Headers.Add("user-agent", "Mozilla/4.0 [...]");
            client.Headers.Add("Content-Type","application/x-www-form-urlencoded");
            try
            {

                byte[] postArray = Encoding.UTF8.GetBytes("somevar=foo&someothervar=bar");
                Uri uri = new Uri("http://www.examplesite.com/somepage.php");

                client.UploadDataCompleted += 
                new UploadDataCompletedEventHandler(client_UploadDataCompleted);
                client.UploadDataAsync(uri, postArray);
            }
            catch (WebException e)
            {
                MessageBox.Show("A regular Web Exception");
            }
            catch (NotSupportedException ne)
            {
                MessageBox.Show("A super Web Exception");
            }
            return true;
        }

        void client_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            MessageBox.Show("The WebClient request completed");
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我创建一个新的类实例并在此处运行该方法:

Downstream Downstream1 = new Downstream();
Downstream1.StartDownstream();
Run Code Online (Sandbox Code Playgroud)

当我这样做时,表单运行的线程似乎挂起,直到WebClient获得响应.为什么是这样?我已经使用过该UploadDataAsync方法,所以它不应该是异步的吗?

编辑: …

c# multithreading asynchronous webclient

5
推荐指数
1
解决办法
5547
查看次数

非阻止下载

我是Windows Phone 7开发的新手,如果你愿意,我在查找如何在'后台'中下载一些数据时会遇到一些麻烦.我知道这是可能的,因为像ESPN等应用程序显示"正在加载... ..".在下载他们的数据时,UI仍然完全响应.我想要做的是下载一些Twitter数据.

这是我现在拥有的,但它阻止了atm:

// Constructor:

// load the twitter data
WebClient twitter = new WebClient();

twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=badreligion"));

// Callback function:

void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
  if (e.Error != null)
  {
    return;
  }

  XElement xmlTweets = XElement.Parse(e.Result);

  TwitterListBox.ItemsSource = from tweet in xmlTweets.Descendants("status")
                               select new TwitterItem
                               {
                                 ImageSource = tweet.Element("user").Element("profile_image_url").Value,
                                 Message = tweet.Element("text").Value,
                                 UserName = tweet.Element("user").Element("screen_name").Value
                               };

}
Run Code Online (Sandbox Code Playgroud)

编辑:尝试多线程:

// in constructor
Dispatcher.BeginInvoke(new ThreadStart(StartTwitterUpdate));

// other functions
private void StartTwitterUpdate()
{
  // load …
Run Code Online (Sandbox Code Playgroud)

c# webclient windows-phone-7

2
推荐指数
1
解决办法
992
查看次数