为什么我的线程没有做任何事情?

时间:2014-06-26 11:46:35

标签: vb.net multithreading

我在名为Fetch.vb的表单中有以下代码:

Imports System.ComponentModel ' might not be needed?
Imports System.Threading

Public Class Fetch

Public Sub New()
    InitializeComponent()
    backgroundWorker1.WorkerReportsProgress = True
    backgroundWorker1.WorkerSupportsCancellation = True
End Sub

 Private Sub btnFetch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetch.Click
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Text.Length = (Not 0) Then
                tbList.Add(ctrl.Text)
                MsgBox(tbList.Item(0).ToString)
                Exit For
            End If

        End If
    Next

    ' ProcessLinks()
    btnFetch.Enabled = False
    BackgroundWorker1.RunWorkerAsync()
End Sub

Public Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    AddHandler BackgroundWorker1.DoWork, AddressOf backgroundWorker1_DoWork
    ProcessLinks()
End Sub

End Class

现在进程链接是一个带有public sub的模块,其中包含我正在尝试运行的代码,我不需要传递给它的任何参数,并且它没有做任何(我认为)可能影响这个的事情,我想我只是在做错的线程代码。我的fetch.vb格式中有backgroundworker1,当我点击btn Fetch时,程序什么都不做。

非常感谢任何帮助和指导或阅读材料。

编辑这是我的LinkProcess模块​​。

Public Module LinkProcess

Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Fetch.BackgroundWorker1.DoWork
    ProcessLinks()
End Sub

    Public Sub ProcessLinks()
    Dim tbContent As String

        For Each tbContent In Fetch.tbList
                Process.Start(tbContent)
        Next


    End Sub
End Module

1 个答案:

答案 0 :(得分:1)

您的ProcessLinks()方法有何功能?它是否访问任何UI元素 - 比如在标签中设置一些文本,或者在TextBox中添加文本等等?如果是这种情况,那就行不通了。您不应该从BackgroundWorker DoWork中访问UI元素。

这是我写的一篇关于如何正确使用BackgroundWorker的小帖子。这可能对你有帮助。 http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker