Vb.net 需要帮助重写foreach和do-until循环吗

Vb.net 需要帮助重写foreach和do-until循环吗,vb.net,foreach,Vb.net,Foreach,所以我试图重新编码,因为它不能正常工作: 如您所见,上面的代码首先拆分“|”,并将它们分别添加到列表中。 然后我们分割其他内容,使其成为一个完美的url,然后通过webkitbrowser导航到它。。。并使用thread.sleep(Idk,如果可以的话)等待15秒,然后在foreach和do-until循环中将其从链接列表中删除。那有什么问题?它不能正常工作WebKit浏览器只是挂起了 注意:加载表单时会调用子StartTrafficExchange() 有人能告诉我上面的代码有什么问题吗?有

所以我试图重新编码,因为它不能正常工作:

如您所见,上面的代码首先拆分“|”,并将它们分别添加到列表中。 然后我们分割其他内容,使其成为一个完美的url,然后通过webkitbrowser导航到它。。。并使用thread.sleep(Idk,如果可以的话)等待15秒,然后在foreach和do-until循环中将其从链接列表中删除。那有什么问题?它不能正常工作WebKit浏览器只是挂起了

注意:加载表单时会调用子StartTrafficExchange()

有人能告诉我上面的代码有什么问题吗?有没有其他方法可以让它工作


-谢谢-

首先,这不是很重要,但代码不一致,您使用的是
C#
实践,但您使用的是
VB.NET
,然后删除字符串附件上的所有
+
运算符,并用
&
替换它们

其次,如果您的目的是等待网页完全加载,那么使用
Sleep
方法是不行的,您需要使用浏览器控件的
WebBrowserDocumentCompleted
事件(我不知道
WebKitBrowser
的确切事件名称)

您可以编写一个名为
NavigateAndWait
的方法,并使用它而不是使用
WebKitBrowser.Navigate
方法来简化操作

我给您举一个默认
WebBrowser
控件的示例:

Private WebPageLoaded As Boolean = False

''' <summary>
''' Navigates to an url and waits the page to be loaded.
''' </summary>
''' <param name="url">Indicates the url to navigate.</param>
''' <param name="newWindow">Indicates whether the url should open into a new browser window.</param>
Private Sub NavigateAndWait(ByVal Browser As WebBrowser,
                           ByVal url As String,
                           Optional newWindow As Boolean = False)

    Me.WebPageLoaded = False

    AddHandler Browser.DocumentCompleted, AddressOf WebBrowserDocumentCompleted
    Browser.Navigate(url, newWindow)

    Do Until Me.WebPageLoaded
        Application.DoEvents()
    Loop

    RemoveHandler Browser.DocumentCompleted, AddressOf WebBrowserDocumentCompleted

End Sub

' WebBrowser [DocumentCompleted]
Private Sub WebBrowserDocumentCompleted(ByVal sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Me.WebPageLoaded = True
End Sub

假设
linklist
不是实际的
LinkedList
,而是一个
List(Of T)
或列表控件的列表,则可以替换此代码块:

    Dim filterstring As String() = downloadstring.ReadToEnd.Split("|")
    For Each stirngman As String In filterstring
        linklist.Items.Add(stirngman)
    Next
为此:

Dim filter As String() = downloadstring.ReadToEnd.Split("|")
linklist.AddRange(filter)
它做同样的事情,只是速度更快,占用的内存更少

接下来,向上移动(如紧跟在
AddRange
之后):

我本打算输入为什么要进行各种更改,但下面是我将要重构的整个例程:

Private Sub StartTrafficExchange()
  ' Performing one split here that removes everything that needs
  ' to go will be faster, take less memory, won't have to be
  ' touched as many times
  Dim filter As String() = downloadstring.ReadToEnd.Split("``[TIER4]|")
  linklist.AddRange(filter)

  ' Set your maximum value so it knows when it is full
  ProgressBarX1.Maximum = linklist.Items.Count

  ' Create a counter to let us know how many items 
  ' have been processed
  Dim counter As Int = 0
  Dim currentURL As String = String.Empty

  Do While linklist.Items.Count > 0
    ' Get the current URL from our list
    currentURL = linklist.Items(counter)

    LabelX1.Text = String.Format("Navigating To [TIER4] {0}", currentURL)
    LabelX1.Refresh() ' Allow the label to update

    ' This is only good if you have a debugger turned on
    ' or are running from the IDE
    Debug.WriteLine(currentURL)

    WebkitBrowser1.Navigate(currentURL)

    ' Not a good idea as it will block the UI
    ' from responding. If you need a delay that
    ' doesn't appear to lock up the UI, implement
    ' a timer in a loop
    Thread.Sleep(15000)

    ' Increment the counter, then update the progressbar
    counter += 1
    ProgressBarX1.Value = counter
    ProgressBarX1.Refresh() ' Allow the progressbar to update
  Loop

  ' When you are through getting all of the URLs,
  ' clear the list just one time
  linklist.Items.Clear()

  ' Make recursive call to this sub
  ' You should limit the number of recursions
  ' somehow so you don't run out of stack space
  StartTrafficExchange
End Sub
如果要暂停进程并阻止UI,请使用以下命令:

' Create a new timer object that will run for 1/10 of second
Dim timr As New Timer(100)

' Run this for 150 times at .1 seconds will
' give you a 15 second pause and still leave
' the UI responsive
For iLoop As Integer = 0 To 150
  ' Start the timer for .1 seconds
  timr.Start()

  ' This tells everything on the form to process updates
  DoEvents()
Next

这是因为你的Thread.Sleep()你的webkitbrowser是免费的。似乎有很多非常相似的字符串处理可以将数组拆分多次,而一次就可以了。谢谢,伙计。但是你能告诉我如何在一个循环中实现计时器吗?我就是这么做的。查看更新。用下面的示例代码替换这一行
Thread.Sleep(15000)
。与在循环中使用计时器相比,它不能像新计时器(15000)一样变暗吗?可以,但它实际上与
Thread.Sleep(15000)
相同。您需要较短的时间运行
DoEvents()
,以允许应用程序处理可能同时发生的任何其他事情。请尝试在Dim语句中将计时器更改为System.Timers.Timer。谢谢,但在我的情况下,我不应该只等待站点加载,而应该等待大约15-20秒。
Dim filter As String() = downloadstring.ReadToEnd.Split("|")
linklist.AddRange(filter)
ProgressBarX1.Maximum = linklist.Items.Count
Private Sub StartTrafficExchange()
  ' Performing one split here that removes everything that needs
  ' to go will be faster, take less memory, won't have to be
  ' touched as many times
  Dim filter As String() = downloadstring.ReadToEnd.Split("``[TIER4]|")
  linklist.AddRange(filter)

  ' Set your maximum value so it knows when it is full
  ProgressBarX1.Maximum = linklist.Items.Count

  ' Create a counter to let us know how many items 
  ' have been processed
  Dim counter As Int = 0
  Dim currentURL As String = String.Empty

  Do While linklist.Items.Count > 0
    ' Get the current URL from our list
    currentURL = linklist.Items(counter)

    LabelX1.Text = String.Format("Navigating To [TIER4] {0}", currentURL)
    LabelX1.Refresh() ' Allow the label to update

    ' This is only good if you have a debugger turned on
    ' or are running from the IDE
    Debug.WriteLine(currentURL)

    WebkitBrowser1.Navigate(currentURL)

    ' Not a good idea as it will block the UI
    ' from responding. If you need a delay that
    ' doesn't appear to lock up the UI, implement
    ' a timer in a loop
    Thread.Sleep(15000)

    ' Increment the counter, then update the progressbar
    counter += 1
    ProgressBarX1.Value = counter
    ProgressBarX1.Refresh() ' Allow the progressbar to update
  Loop

  ' When you are through getting all of the URLs,
  ' clear the list just one time
  linklist.Items.Clear()

  ' Make recursive call to this sub
  ' You should limit the number of recursions
  ' somehow so you don't run out of stack space
  StartTrafficExchange
End Sub
' Create a new timer object that will run for 1/10 of second
Dim timr As New Timer(100)

' Run this for 150 times at .1 seconds will
' give you a 15 second pause and still leave
' the UI responsive
For iLoop As Integer = 0 To 150
  ' Start the timer for .1 seconds
  timr.Start()

  ' This tells everything on the form to process updates
  DoEvents()
Next