HttpWebRequest.BeginGetResponse 不是异步的
我有一些代码可以迭代 100 个 url 并从网络请求数据。
看起来像这样
for each url in urls
Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
Dim rq = New ReqArgs
rq.Url= url
rq.Request = hwr
Dim res =
hwr.BeginGetResponse(New AsyncCallback(AddressOf FinishWebRequest), rq)
Dim a = 1
next
这看起来不错吗?
为什么 BeginGetresponse
行在进入 dim a=1
之前需要大约 2-3 秒才能完成?
实际上,我进行了调试,发现 FinishWebRequest
过程在到达 Dim a=1
之前完全运行。
那么这是异步的吗?
我没有通过使用异步来赚取任何时间。我是吗?或者有其他方法可以做到这一点吗?
要点是,主子程序应该发出 300 个请求并将控制权返回给 UI,然后随着请求的到来,FinishWebRequest
应该在自己的线程和自己的时间上缓慢地处理它们。
我该如何做这样做吗?
顺便说一句,主子程序在 BackgroundWorker
中运行,但我检查了 BackgroundWorker
,问题是相同的
似乎答案应该是 此处 但它对我不起作用,
我是 WPF 4.0
感谢您的帮助和建议。谢谢
I have some code that iterates a few 100 urls and requests the data from the web.
It looks something like this
for each url in urls
Dim hwr = CType(WebRequest.Create(url), HttpWebRequest)
Dim rq = New ReqArgs
rq.Url= url
rq.Request = hwr
Dim res =
hwr.BeginGetResponse(New AsyncCallback(AddressOf FinishWebRequest), rq)
Dim a = 1
next
Does this look ok?
How come the BeginGetresponse
line takes about 2-3 seconds to complete before going to dim a=1
?.
Actually I debugged and I see that the FinishWebRequest
procedure runs completely before the Dim a=1
is reached.
So is this async?
I'm not earning any time by using the async. am I? Or is there a different way to do this?
The point is that the main sub should fire off 300 requests and return control to the UI, then the FinishWebRequest
should process them slowly on its own thread and own time , as the requests come in.
How do I do that?
Btw, the main sub is running in a BackgroundWorker
, but I checked with out the BackgroundWorker
and the problem is the same
It seems that the answer should be here but its just not working for me
I'm WPF 4.0
Appreciate your help and advice. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,
问题出在 POST 上,
我现在开始像这样写帖子
,然后我的回调就像这样
希望这对将来的人有帮助
yup
the problem was with the POST
i now start the post writing like this
and then my callback is like this
hope this helps someone in the future
从另一个问题重新发布此问题。
来自 HttpWebRequest 文档.BeginGetResponse方法:
为了避免等待设置,您可以使用
HttpWebRequest.BeginGetRequestStream 方法< /a>
但请注意:
Reposting this from another question.
From the documentation on HttpWebRequest.BeginGetResponse Method:
To avoid waiting for the setup, you can use
HttpWebRequest.BeginGetRequestStream Method
but be aware that: