![]() |
|
how to make it work automatically ? - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: how to make it work automatically ? (/Thread-how-to-make-it-work-automatically) |
how to make it work automatically ? - polas - 07-21-2011 Hello. Do you know how to work backgroundworker with this code? The request would be as I pulled out of google with a background worker and if so it is possible to collect links from google pagination. Not in label or how I'm new for that. I just want To make go to next page like 1,2,3,4,5,6,7,8,9 and so on clicking button and make it go itself and pick up links through all pages with background worker or timer or how to make it work automatically as i said i'm new. Like google has pagination. It only goes through one page and stop on first number and do not move far. Thank you. Code: Private Sub WebBrowser1_DocumentCompleted (ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState WebBrowserReadyState.Complete =) Then
For Each ClientControl I HtmlElement In WebBrowser1.Document.Links
IF NOT ClientControl.GetAttribute ("href"). Contains (Google) And ClientControl.GetAttribute ("href"). Contains ("http") and ClientControl.GetAttribute ("href"). Contains ("http") Then
ListBox1.Items.Add (ClientControl.GetAttribute ('href'))
end If
Next
end If
end Sub
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate (http://www.google.lt/search?num=100&q = "& ComboBox1.Text)
BackgroundWorker1.RunWorkerAsync ()
end Sub
Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 to 100
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end Sub
Private Sub BackgroundWorker1_ProgressChanged (ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Label1.Text = e.ProgressPercentage
end SubIf do not understand what i mean to say so here is the example of that i want to get.
how to make it work automatically ? - polas - 07-21-2011 Hello. Do you know how to work backgroundworker with this code? The request would be as I pulled out of google with a background worker and if so it is possible to collect links from google pagination. Not in label or how I'm new for that. I just want To make go to next page like 1,2,3,4,5,6,7,8,9 and so on clicking button and make it go itself and pick up links through all pages with background worker or timer or how to make it work automatically as i said i'm new. Like google has pagination. It only goes through one page and stop on first number and do not move far. Thank you. Code: Private Sub WebBrowser1_DocumentCompleted (ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState WebBrowserReadyState.Complete =) Then
For Each ClientControl I HtmlElement In WebBrowser1.Document.Links
IF NOT ClientControl.GetAttribute ("href"). Contains (Google) And ClientControl.GetAttribute ("href"). Contains ("http") and ClientControl.GetAttribute ("href"). Contains ("http") Then
ListBox1.Items.Add (ClientControl.GetAttribute ('href'))
end If
Next
end If
end Sub
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate (http://www.google.lt/search?num=100&q = "& ComboBox1.Text)
BackgroundWorker1.RunWorkerAsync ()
end Sub
Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 to 100
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end Sub
Private Sub BackgroundWorker1_ProgressChanged (ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Label1.Text = e.ProgressPercentage
end SubIf do not understand what i mean to say so here is the example of that i want to get.
RE: how to make it work automatically ? - blowdoof - 07-21-2011 that's because you call the backgroundworker once, and you only tell it to wait (100 times) and provide feedback on the (wait) progress.. nothing else is defined in the backgroundprocess... One option would be calling Code: WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)so the entire sub would look like this.. Code: Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 to 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end SubHOWEVER, a better solution, instead of calling the thread.sleep each time (since you can't be sure how long it takes to load the page), would be calling it at the end of your WebBrowser1.DocumentCompleted routine... In that case, you will have to pass trough or make the counter variable available to the routine.. f.e. Code: private i as integer = 1
Private Sub WebBrowser1_DocumentCompleted (ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState WebBrowserReadyState.Complete =) Then
For Each ClientControl I HtmlElement In WebBrowser1.Document.Links
IF NOT ClientControl.GetAttribute ("href"). Contains (Google) And ClientControl.GetAttribute ("href"). Contains ("http") and ClientControl.GetAttribute ("href"). Contains ("http") Then
ListBox1.Items.Add (ClientControl.GetAttribute ('href'))
end If
Next
end If
if not i > 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)
end if
end Sub.. because you call the 'navigate' at the end, the same routine will be called again (szince that page also invokes the WebBrowser1.DocumentCompleted event...) RE: how to make it work automatically ? - blowdoof - 07-21-2011 that's because you call the backgroundworker once, and you only tell it to wait (100 times) and provide feedback on the (wait) progress.. nothing else is defined in the backgroundprocess... One option would be calling Code: WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)so the entire sub would look like this.. Code: Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 to 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end SubHOWEVER, a better solution, instead of calling the thread.sleep each time (since you can't be sure how long it takes to load the page), would be calling it at the end of your WebBrowser1.DocumentCompleted routine... In that case, you will have to pass trough or make the counter variable available to the routine.. f.e. Code: private i as integer = 1
Private Sub WebBrowser1_DocumentCompleted (ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState WebBrowserReadyState.Complete =) Then
For Each ClientControl I HtmlElement In WebBrowser1.Document.Links
IF NOT ClientControl.GetAttribute ("href"). Contains (Google) And ClientControl.GetAttribute ("href"). Contains ("http") and ClientControl.GetAttribute ("href"). Contains ("http") Then
ListBox1.Items.Add (ClientControl.GetAttribute ('href'))
end If
Next
end If
if not i > 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & ComboBox1.Text)
end if
end Sub.. because you call the 'navigate' at the end, the same routine will be called again (szince that page also invokes the WebBrowser1.DocumentCompleted event...) RE: how to make it work automatically ? - polas - 07-21-2011 I get error when i try enter some keyword in combobox and get this. How to fix it. http://s2.postimage.org/runc91sd/i_get_error_on_i.png Or maybe there is another way to make it work automatically ? Besause in this way doea not work ![]() Thanks. RE: how to make it work automatically ? - polas - 07-21-2011 I get error when i try enter some keyword in combobox and get this. How to fix it. http://s2.postimage.org/runc91sd/i_get_error_on_i.png Or maybe there is another way to make it work automatically ? Besause in this way doea not work ![]() Thanks. RE: how to make it work automatically ? - blowdoof - 07-21-2011 If your background operation requires a parameter, call RunWorkerAsync with your parameter. Inside the DoWork event handler, you can extract the parameter from the DoWorkEventArgs.Argument property. so try this; Code: Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate (http://www.google.lt/search?num=100&q = "& ComboBox1.Text)
dim parameter as string = ComboBox1.Text
BackgroundWorker1.RunWorkerAsync(parameter)
end Subthen change your asyncworker: Code: Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim path As String = DirectCast(e.Argument, String)
For i = 0 to 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & path)
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end SubRE: how to make it work automatically ? - blowdoof - 07-21-2011 If your background operation requires a parameter, call RunWorkerAsync with your parameter. Inside the DoWork event handler, you can extract the parameter from the DoWorkEventArgs.Argument property. so try this; Code: Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate (http://www.google.lt/search?num=100&q = "& ComboBox1.Text)
dim parameter as string = ComboBox1.Text
BackgroundWorker1.RunWorkerAsync(parameter)
end Subthen change your asyncworker: Code: Private Sub BackgroundWorker1_DoWork (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim path As String = DirectCast(e.Argument, String)
For i = 0 to 100
WebBrowser1.Navigate (http://www.google.lt/search?num=" & i & "&q = " & path)
Threading.Thread.Sleep (200)
BackgroundWorker1.ReportProgress (i)
Next
end SubRE: how to make it work automatically ? - polas - 07-21-2011 Not working just after 2 minutes it show only blank google page ![]() I did everything but no luck. It doesn't even extract links. Just stop for 2 minutes and show google page. no error but and not working. I do not understand how to chnage i'm new so i see this asyncworker first time so how to change it ? ![]() Allright what em i doing wrong can you fix this here is my code. Maybe now you will what is the problem with code or i'm doing something wrong. Really thank you in advance. Code: Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState = WebBrowserReadyState.Complete) Then
For Each ClientControl As HtmlElement In WebBrowser1.Document.Links
If Not ClientControl.GetAttribute("href").Contains("google") And ClientControl.GetAttribute("href").Contains("http") And ClientControl.GetAttribute("href").Contains("http") Then
ListBox1.Items.Add(ClientControl.GetAttribute("href"))
End If
Next
End If
'Remove dups
ListBox1.Sorted = True
ListBox1.Refresh()
Dim index As Integer
Dim itemcount As Integer = ListBox1.Items.Count
If itemcount > 1 Then
Dim lastitem As String = ListBox1.Items(itemcount - 1)
For index = itemcount - 2 To 0 Step -1
If ListBox1.Items(index) = lastitem Then
ListBox1.Items.RemoveAt(index)
Else
lastitem = ListBox1.Items(index)
End If
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://www.google.lt/search?num=100&q = " & ComboBox1.Text)
Dim parameter As String = ComboBox1.Text
BackgroundWorker1.RunWorkerAsync(parameter)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If (ListBox1.SelectedIndex <> -1) Then _
Process.Start(ListBox1.SelectedItem.ToString())
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
WebBrowser1.Navigate(ComboBox1.Text)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
WebBrowser1.Focus()
SendKeys.Send("^f")
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim path As String = DirectCast(e.Argument, String)
For i = 0 To 100
WebBrowser1.Navigate("http://www.google.lt/search?num=" & i & "&q = " & path)
Threading.Thread.Sleep(200)
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub ZoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomToolStripMenuItem.Click
WebBrowser1.Document.ForeColor = Color.Chocolate
End SubRE: how to make it work automatically ? - polas - 07-21-2011 Not working just after 2 minutes it show only blank google page ![]() I did everything but no luck. It doesn't even extract links. Just stop for 2 minutes and show google page. no error but and not working. I do not understand how to chnage i'm new so i see this asyncworker first time so how to change it ? ![]() Allright what em i doing wrong can you fix this here is my code. Maybe now you will what is the problem with code or i'm doing something wrong. Really thank you in advance. Code: Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.ReadyState = WebBrowserReadyState.Complete) Then
For Each ClientControl As HtmlElement In WebBrowser1.Document.Links
If Not ClientControl.GetAttribute("href").Contains("google") And ClientControl.GetAttribute("href").Contains("http") And ClientControl.GetAttribute("href").Contains("http") Then
ListBox1.Items.Add(ClientControl.GetAttribute("href"))
End If
Next
End If
'Remove dups
ListBox1.Sorted = True
ListBox1.Refresh()
Dim index As Integer
Dim itemcount As Integer = ListBox1.Items.Count
If itemcount > 1 Then
Dim lastitem As String = ListBox1.Items(itemcount - 1)
For index = itemcount - 2 To 0 Step -1
If ListBox1.Items(index) = lastitem Then
ListBox1.Items.RemoveAt(index)
Else
lastitem = ListBox1.Items(index)
End If
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://www.google.lt/search?num=100&q = " & ComboBox1.Text)
Dim parameter As String = ComboBox1.Text
BackgroundWorker1.RunWorkerAsync(parameter)
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If (ListBox1.SelectedIndex <> -1) Then _
Process.Start(ListBox1.SelectedItem.ToString())
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
WebBrowser1.Navigate(ComboBox1.Text)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
WebBrowser1.Focus()
SendKeys.Send("^f")
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim path As String = DirectCast(e.Argument, String)
For i = 0 To 100
WebBrowser1.Navigate("http://www.google.lt/search?num=" & i & "&q = " & path)
Threading.Thread.Sleep(200)
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub ZoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomToolStripMenuItem.Click
WebBrowser1.Document.ForeColor = Color.Chocolate
End Sub |