如何使列表框的for循环与每个暂停之间


Public Class Form1
    Dim Iclick, submit
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Iclick.InvokeMember("click")
        With WebBrowser1.Document
            For l_index As Integer = 0 To ListBox1.Items.Count - 1
                Dim l_text As String = CStr(ListBox1.Items(l_index))
                .All("input").InnerText = l_text
                System.Threading.Thread.Sleep(5000)
            Next
            '.All("input").InnerText = "http://wordpress.com"
        End With
        submit.InvokeMember("click")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://whatwpthemeisthat.com")
    End Sub
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Iclick = WebBrowser1.Document.GetElementById("input")
        submit = WebBrowser1.Document.GetElementById("check")
    End Sub
End Class

这是我的代码到目前为止,我有一个ListBox与url我想检查使用web浏览器哪个主题是他们运行(如果他们是wordpress),但程序似乎是bug当我点击启动它不响应,直到最后一个元素。它必须与system。threading。thread。sleep行有关但我不知道我做错了什么?谢谢。

这正是Thread.Sleep()方法的作用。它会冻结5秒。此外,每次重复For循环时,都要替换输入。所以它替换,等待5秒,替换,等待5秒。等等。

我猜你是想为每个元素点击提交按钮,但那不是你在做的。每次文本更改时,您都必须点击提交按钮。但是你真的不能确定页面的加载时间

我建议您将提交部分放在循环中,然后等待,然后进入下一次迭代。也许你甚至可以尝试多次运行网站,每个列表框项一次,然后将正确的文本应用到网页中的控件中,点击按钮并收到结果。

编辑:对于thread . sleep(),最好的选择是创建一个新线程,在其中放置循环。这个线程可以暂停5秒,您的应用程序仍然会响应。下面是创建一个目录的方法:
    Imports System.Threading.Thread
    Dim myThread as Thread
    '//..........
    '//Button1 is clicked ->
    myThread = new Thread (AddressOf myLoop)
    myThread.start()
    '//..........
    Private Sub myLoop ()
    '// Loop goes here...
    'Sleeping here will only affect the thread that runs this sub. Your form will still be available
    End Sub