变量未设置或循环无法正常工作

时间:2016-07-12 00:44:09

标签: vb.net multithreading

我遇到了将变量设置为函数返回值的问题,我似乎无法让它工作,我甚至没有得到视觉工作室的错误或反馈在严格的模式

Imports System.Net
Imports System.IO
Imports System.ComponentModel

Public Class Form2
    Dim i As Integer
    Public CleanSearchTexts As String()
    Dim count As Integer = 0
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If BackgroundWorker1.IsBusy = False Then
            i = RichTextBox1.Lines.Count
            i = i - 1
            BackgroundWorker1.RunWorkerAsync()
        Else
            MsgBox("Threads are busy")
        End If
    End Sub

    Private Sub StartThreads()
        Dim SearchText As String
        For count = count To i
            SearchText = LineFunc(count)
            count += 1
            SearchText = CType(Ask_Query(SearchText), String)
            SearchText = CType(Bing_Query(SearchText), String)
            SearchText = CType(Yahoo_Query(SearchText), String)
            Dim thread_count As String = CType(Process.GetCurrentProcess().Threads.Count - 20, String)
            Label_T(thread_count)
            Threading.Thread.Sleep(500)
            If SearchText.Contains("All_Query:Yes") Then
                SearchText = SearchText.Replace("All_Query:Yes", "")
                RTB(SearchText)
            End If
        Next
    End Sub

    Private Delegate Sub UpdateStatus(ByVal s As String)
    Private Delegate Sub UpdateLabel(ByVal thread_count As String)
    Public Delegate Function GetTextBox(ByVal index As Integer) As String

    Public Function LineFunc(ByVal index As Integer) As String
        If InvokeRequired Then
            Invoke(New GetTextBox(AddressOf LineFunc), index)
        Else
            Dim indexSearchText As String
            indexSearchText = RichTextBox1.Lines(index)
            Return indexSearchText
        End If
    End Function


    Public Sub RTB(ByVal s As String)
        If Me.InvokeRequired Then
            Me.Invoke(New UpdateStatus(AddressOf RTB), New Object() {s})
        Else
            RichTextBox2.AppendText(Environment.NewLine & s)
        End If

    End Sub

    Public Sub Label_T(ByVal thread_count As String)
        If Me.InvokeRequired Then
            Me.Invoke(New UpdateLabel(AddressOf Label_T), New Object() {thread_count})
        Else
            Label3.Text = "Threads Running:   " + thread_count
        End If

    End Sub


    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.WorkerReportsProgress = True

        Dim count As Integer
        Dim num As Integer = CInt(TextBox1.Text) - 1
        For count = 0 To num
            Dim thread = New Threading.Thread(AddressOf StartThreads)
            thread.IsBackground = True
            thread.Name = "Web Thread #" + CType(count, String)
            thread.Start()
            Threading.Thread.Sleep(500)
        Next

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MsgBox("Work is done")
    End Sub
End Class

问题显然与行获取功能或计数有关 我已经通过添加一个按钮来测试该功能,并执行将变量设置为返回和msgboxing它的过程,并且它完美地工作,不仅当我断点它它显示它返回正确的输出但它只是没有设置无论出于何种原因,这个子

    Public Sub StartThreads(ByVal List As Object)
    Dim List2 As String() = CType(List, String())
    For Each dork As String In List2
        dork = CType(Ask_Query(dork), String)
        dork = CType(Bing_Query(dork), String)
        dork = CType(Yahoo_Query(dork), String)
        Dim thread_count As String = CType(Process.GetCurrentProcess().Threads.Count - 20, String)
        Label_T(thread_count)
        Threading.Thread.Sleep(500)
        If dork.Contains("All_Query:Yes") Then
            dork = dork.Replace("All_Query:Yes", "")
            RTB(dork)
        End If
    Next
End Sub

这是函数

Public Delegate Function GetTextBox(ByVal index As Integer) As String

Public Function LineFunc(ByVal index As Integer) As String
    If InvokeRequired Then
        Invoke(New GetTextBox(AddressOf LineFunc), index)
    Else
        Dim indexSearchText As String
        indexSearchText = RichTextBox1.Lines(index)
        Return indexSearchText
    End If
End Function

更新:列出拆分功能

Public Function splitData(ByVal width As Integer, ByVal dd As List(Of String)) As List(Of List(Of String))

    Dim dds As New List(Of List(Of String))


    Dim numberOfLists As Integer = (dd.Count \ width)

    For i As Integer = 0 To numberOfLists
        Dim newdd As List(Of String)
        newdd = dd.Skip(i * width).Take(width).ToList()
        dds.Add(newdd)
    Next i

    Return dds
End Function

更新后台工作人员

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    BackgroundWorker1.WorkerSupportsCancellation = True
    BackgroundWorker1.WorkerReportsProgress = True

    Dim dds As New List(Of String)
    GetList(dds)
    Dim num As Integer = CInt(TextBox1.Text)
    Dim ThreadCount As Integer = CInt(TextBox1.Text)
    If ThreadCount > 1 Then
        ThreadCount -= 1
    End If
    Dim RTBLines As Integer = RTB_Lines()
    num = CInt(RTBLines / num)
    num = CInt(Math.Ceiling(num))
    Dim splitdd As List(Of List(Of String)) = CType(splitData(num, dds), List(Of List(Of String)))
    For count = 0 To ThreadCount
        Dim ListArray As String() = splitdd(count).ToArray
        Dim newthread As New Thread(AddressOf StartThreads)
        newthread.Name = "Web Thread #" + CType(count, String)
        newthread.Start(ListArray)
        Threading.Thread.Sleep(500)
    Next

End Sub

0 个答案:

没有答案