异步方法无法按预期工作
Posted
技术标签:
【中文标题】异步方法无法按预期工作【英文标题】:Async method doesn't work as expected 【发布时间】:2017-04-17 23:11:39 【问题描述】:我正在制作使用 URL 下载流式 mp4 的测试控制台应用程序。
我使用了 MSDN 上发布的异步示例。
下面是代码。
class Program
public static ManualResetEvent allDone = new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
public static int total = 0;
static void Main(string[] args)
Uri httpSite = new Uri("URI");
WebRequest wreq = WebRequest.Create(httpSite);
RequestState rs = new RequestState();
rs.Request = wreq;
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(
new AsyncCallback(RespCallback), rs);
allDone.WaitOne();
FileStream file = new FileStream("test.mp4", FileMode.Append);
file.Write(rs.data.ToArray(), 0, rs.data.Count());
Console.ReadKey();
private static void RespCallback(IAsyncResult ar)
// Get the RequestState object from the async result.
RequestState rs = (RequestState)ar.AsyncState;
// Get the WebRequest from RequestState.
WebRequest req = rs.Request;
// Call EndGetResponse, which produces the WebResponse object
// that came from the request issued above.
WebResponse resp = req.EndGetResponse(ar);
// Start reading data from the response stream.
Stream ResponseStream = resp.GetResponseStream();
// Store the response stream in RequestState to read
// the stream asynchronously.
rs.ResponseStream = ResponseStream;
// Pass rs.BufferRead to BeginRead. Read data into rs.BufferRead
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0,
BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
private static void ReadCallBack(IAsyncResult asyncResult)
// Get the RequestState object from AsyncResult.
RequestState rs = (RequestState)asyncResult.AsyncState;
// Retrieve the ResponseStream that was set in RespCallback.
Stream responseStream = rs.ResponseStream;
// Read rs.BufferRead to verify that it contains data.
int read = responseStream.EndRead(asyncResult);
if (read > 0)
// Prepare a Char array buffer for converting to Unicode.
byte[] charBuffer = new byte[BUFFER_SIZE];
responseStream.Read(charBuffer, 0, read);
rs.data.AddRange(charBuffer);
IAsyncResult ar = responseStream.BeginRead(
rs.BufferRead, 0, BUFFER_SIZE,
new AsyncCallback(ReadCallBack), rs);
else
responseStream.Close();
// Set the ManualResetEvent so the main thread can exit.
allDone.Set();
return;
当我使用这种异步方法时,保存的文件大小是 1,356KB,而文件应该是 2,409KB。
我可以在 responseStream.close() 上的断点被击中后找到 IF(read > 0) 语句中的断点。这是因为它读取的长度为 0,然后读取更多吗?
当我使用如下非异步方法时,我可以成功下载文件。
public static void StreamDownload(string url)
int dataLength;
int bytesRead;
WebRequest req = WebRequest.Create(url);
WebResponse response = req.GetResponse();
string fileName = System.IO.Path.GetFileName("test.mp4");
Stream dataStream = response.GetResponseStream();
byte[] buffer = new byte[1024];
FileStream file = new FileStream(fileName, FileMode.Append);
dataLength = (int)response.ContentLength;
do
bytesRead = dataStream.Read(buffer, 0, buffer.Length);
file.Write(buffer, 0, bytesRead);
while (bytesRead != 0);
有人知道为什么我使用异步方法失败了吗?
感谢您的帮助。
【问题讨论】:
【参考方案1】:还有多种其他方法可以简单得多下载文件,您应该选择其中一种。
webclient vs httpclient vs httpwebrequest
在你的情况下,我建议 HttpClient:
public static async Task DownloadAsync(string requestUri, string filename)
using (var httpClient = new HttpClient())
using (var fs = File.OpenWrite(filename))
await (await httpClient.GetStreamAsync(requestUri)).CopyToAsync(fs);
如果您需要显示一些进度,或者您可以使用 WebClient 类的DownloadFileAsync 方法,但上面的示例更简单。
【讨论】:
以上是关于异步方法无法按预期工作的主要内容,如果未能解决你的问题,请参考以下文章
Promise.all 和 .map 函数的异步/等待无法按预期工作
无法从异步函数获取返回值,等待未按预期工作(Vue API 服务)