等待变量更改VB.Net中状态的更好方法 [英] A better way to wait for a variable to change state in VB.Net
问题描述
我有一个遍历许多值的循环.迭代每个值后,将页面加载到webbrowser控件中(将值作为参数传递),并在加载和读取页面时,循环应转到列表中的下一个值,并继续进行,直到处理完所有值为止.我需要一种方法来在网站异步加载时暂停该过程,然后在页面加载/阅读过程完成后恢复.
I have a loop that goes through a number of values. With every value iterated, a page is loaded in a webbrowser control (with the value passed as a parameter) and when the page is loaded and read, the loop should go to the next value in the list and continue until all values are processed. I need a way to pause the procedure while the website is loading asynchronously and then resume once the page loading/reading process is complete.
我正在做的事情是使用类似这样的东西,其中"ReadingInProgress"是一个全局变量:
The way I am doing is by using something like this, where "ReadingInProgress" is a global variable:
ReadingInProgress = True
wb.Navigate("http://mywebsite.com/mypage.aspx" & c)
While ReadingInProgress
Application.DoEvents()
End While
webrowser控件的"DocumentCompleted"事件将"ReadingInProgress"设置为false,这将导致while循环退出并继续执行该过程.这行得通,但是我意识到这给CPU带来了压力.是否有更好的,较少占用CPU的方法来做到这一点?
The "DocumentCompleted" event of the webrowser control set "ReadingInProgress" to false which causes the while loop to exit and resume the procedure. This works, but I realize that it puts a strain on the CPU. Is there a better, less CPU intensive way to do this?
谢谢!
推荐答案
我最近回答了以非常相似的方式.使用此技术,您将获得自然的代码执行流(DocumentComplete
事件被封装为Task
).
I've recently answered a similar question. The solution is in C#, but you can use Async/Await
in VB.NET in a very similar way. Using this technique, you would get a natural flow of execution for your code (DocumentComplete
event is encapsulated as Task
).
这篇关于等待变量更改VB.Net中状态的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!