0

Hey guys so i'm writing a program that i want to make faster by adding multiple threads

what it does it reads from a richtextbox and parse it to the end of a website then grabs data and if it contains certain string then it adds it to another textbox

Imports System.Net
Imports System.IO
Public Class Form2
Dim i As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    i = RichTextBox1.Lines.Count
    StartThreads()
End Sub

Private Sub StartThreads()
    Dim count As Integer
    For count = 0 To TextBox1.Text
        Dim thread = New Threading.Thread(AddressOf Chk)
        thread.IsBackground = True
        thread.Start()
        Threading.Thread.Sleep(500)
    Next
End Sub


Private Sub Chk()
    While i > 0
        i = i - 1
        Threading.Thread.Sleep(500)
        Application.DoEvents()
        Dim text As String = RichTextBox1.Lines(i).ToString
        Dim request As WebRequest = WebRequest.Create("WWW.WEBSITE.COM" + text)
        Dim responce As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim datastream As Stream = responce.GetResponseStream
        Dim reader As New StreamReader(datastream)
        Dim strData As String = reader.ReadToEnd
        Application.DoEvents()
        If strData.Contains("no-result") Then
            Application.DoEvents()
        Else
            Application.DoEvents()
            RichTextBox2.AppendText(Environment.NewLine & text)
        End If
    End While
End Sub
End Class

I get an Error for Cross Thread operations not valid and i tried reading up on delegates and i get how they work in a basic way but i don't think that will solve my main issue of reading a textbox at the same time while doing the same operation with different values and pasting it to the same (other) textbo also seems like even when i use the nonprofessional way and turn the cross thread check off i get duplicate results which i don't know why either

Babbillumpa
  • 1,854
  • 1
  • 16
  • 21
drukoz
  • 11
  • 4
  • Make a habit of switching Option Strict On - it will help you to spot errors in your code before they happen – Matt Wilko Jul 05 '16 at 10:15
  • **Under no circumstances should you call `Application.DoEvents`.** That code is wrong, take it out immediately. To solve the actual problem, you need to use the `Invoke` method, as described in the series of answers to the linked duplicate. – Cody Gray - on strike Jul 05 '16 at 12:51

0 Answers0