所以我试图重新编码,因为它无法正常工作:
Private Sub StartTrafficExchange() Dim downloadstring As New StreamReader(Response2.GetResponseStream()) Dim filterstring As String() = downloadstring.ReadToEnd.Split("|") For Each stirngman As String In filterstring linklist.Items.Add(stirngman) Next Do Until linklist.Items.Count = 0 Dim rand As New Random linklist.SelectedIndex = rand.Next(0, linklist.Items.Count - 1) Dim strimgna As String = linklist.SelectedItem Dim newlinkstring As String() = strimgna.Split("``") For Each stringma As String In newlinkstring If stringma.Length < 8 Then GoTo a Else LabelX1.Text = "Navigating To " + stringma stringma = stringma.Replace("[TIER4]", "") Debug.WriteLine(stringma) WebKitBrowser1.Navigate(strimgna) Thread.Sleep(15000) End If LabelX1.Text = "Navigating To " + stringma ProgressBarX1.Value += 1 a: Next linklist.Items.Remove(linklist.SelectedItem) Loop ProgressBarX1.Maximum = linklist.Items.Count ProgressBarX1.Value = 0 StartTrafficExchange() End Sub
那么代码是做什么的?好吧,它会将一个webrequest提取到一个链接,其中的源代码就像
http://www.asdfd.com``[TIER4]|http://aesde.com``[TIER4]|http://www.excaedf.com``[TIER4]|
如您所见,上面的代码首先拆分“|”并将它们添加到列表中。 然后我们拆分其他东西,使其成为一个完美的URL,然后通过webkitbrowser导航到它...并使用thread.sleep等待15秒(如果它工作,则为Idk。)然后将其从foreach中的linklist中删除,直到循环。所以有什么问题 ?好吧,它无法正常运行webkitbrowser只是悬挂。
注意:表单加载时会调用子StartTrafficExchange()。
有人可以告诉我上面的代码有什么问题,是否有其他方法可以使这项工作?
-Thanks -
答案 0 :(得分:1)
首先,这不是很重要,但代码不一致,您使用C#
做法,但是您在VB.NET
,然后删除所有+
运算符上的字符串追加并用&
替换它们。
其次,如果您的目的是等待网页完全加载,那么使用Sleep
方法不是这样的,您需要像浏览器控件的WebBrowserDocumentCompleted
事件一样(I不知道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
其他用于GeckoFX
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>
Private Sub NavigateAndWait(Byval Browser as Gecko.GeckoWebBrowser,
Byval url As String,
Optional loadFlags As Gecko.GeckoLoadFlags = Gecko.GeckoLoadFlags.None,
Optional referrer As String = Nothing,
Optional postData As Gecko.GeckoMIMEInputStream = Nothing)
Me.WebPageLoaded = False
AddHandler Browser.DocumentCompleted, AddressOf GeckoWebBrowserDocumentCompleted
Browser.Navigate(url, loadFlags, referrer, postData)
Do Until Me.WebPageLoaded
Application.DoEvents()
Loop
RemoveHandler Browser.DocumentCompleted, AddressOf GeckoWebBrowserDocumentCompleted
End Sub
' GeckoWebBrowser [DocumentCompleted]
Private Sub GeckoWebBrowserDocumentCompleted(ByVal sender As Object, e As EventArgs)
Me.WebPageLoaded = True
End Sub
答案 1 :(得分:1)
假设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
之后):
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
如果要暂停阻止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