首页 新闻 搜索 专区 学院

仍然提问。await async偶尔死在readasync上是何道理?(源码已精简)

0
悬赏园豆:60 [已关闭问题] 解决于 2014-09-15 07:38

代码精简过了。网速慢的时候就死掉了。我刻意找了个国外网站。并且开着迅雷下东西。

  async void button1_Click(object sender, EventArgs e)
        {
            while (!exit)
            {
                try
                {
                    var req = WebRequest.CreateHttp("http://www.cn431.com/cmvejhe/");// as HttpWebRequest;
                    using (var res = await req.GetResponseAsync() as HttpWebResponse)
                    {
                        using (var ms = new MemoryStream())
                        {
                            using (var stream = res.GetResponseStream())
                            {
                                //await stream.CopyToAsync(ms);//.Timeout(TimeSpan.FromMilliseconds(RWTimeout),"获取数据超时");
                                System.Diagnostics.Trace.WriteLine("read");
                                byte[] buffer = new byte[1024];
                                int count = 0;
                                while (0 < (count = await stream.ReadAsync(buffer, 0, buffer.Length)))
                                {
                                    //int count = await stream.ReadAsync(buffer, 0, buffer.Length);
                                    /*if (count == 0)
                                    {
                                        return;
                                    }*/
                                    System.Diagnostics.Trace.WriteLine("write" + count.ToString());
                                    await ms.WriteAsync(buffer, 0, count);
                                }
                                //  bytes= ms.ToArray();
                            }
                        }
                    }

                }
                catch (Exception ex) { }
            }
        }


大家以一试。Trace.WriteLine write后。在下一次读之后。程序没任何反映了。也不卡界面

gnhao的主页 gnhao | 初学一级 | 园豆:9
提问于:2013-07-16 12:48
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册