gpt4 book ai didi

c# BeginGetResponse : is there any reason to read bytes asynchronously in response callback? 回复

转载 作者:行者123 更新时间:2023-11-30 22:38:33 26 4
gpt4 key购买 nike

BeginGetResponse 的 MSDN 示例代码有两个异步调用,一个是获取响应,和一个从响应缓冲区读取:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx

看到这一行:

// Begin the Reading of the contents of the HTML page and print it to the console.
IAsyncResult asynchronousInputRead = responseStream.BeginRead(myRequestState.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);

我的问题是:与同步读取相比,这里的优势是什么?

谢谢!

最佳答案

以下是我从进一步调查中收集到的信息:即使回调发生在非 UI 线程上,异步读取也会阻止该线程阻塞。当线程被阻塞时,它不能被线程池重新使用,并且另一个并发请求可能会导致线程池创建更多线程,这会产生一些开销。

关于c# BeginGetResponse : is there any reason to read bytes asynchronously in response callback? 回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com