代码精简过了。网速慢的时候就死掉了。我刻意找了个国外网站。并且开着迅雷下东西。
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后。在下一次读之后。程序没任何反映了。也不卡界面