vb.net程序未在新线程上正确启动

vb.net程序未在新线程上正确启动,vb.net,multithreading,Vb.net,Multithreading,我正在做一个项目,有时需要启动其他项目。它将启动的程序都是自定义的,因此没有可引用的IOP类 理论上可能需要同时运行多个程序,所以我尝试将启动的每个程序放在它自己的线程上。这是我第一次尝试多线程,所以我很确定我在这方面失败了,但我不确定在哪里 要启动程序,我使用以下代码 Try Dim nt As New ExecThreadObj 'Fill in the properties nt.Status = "Running" nt.TaskName = spo.N

我正在做一个项目,有时需要启动其他项目。它将启动的程序都是自定义的,因此没有可引用的IOP类

理论上可能需要同时运行多个程序,所以我尝试将启动的每个程序放在它自己的线程上。这是我第一次尝试多线程,所以我很确定我在这方面失败了,但我不确定在哪里

要启动程序,我使用以下代码

Try
    Dim nt As New ExecThreadObj

    'Fill in the properties
    nt.Status = "Running"
    nt.TaskName = spo.Name
    nt.prog = spo.Prog
    nt.STime = Now
    nt.T = New System.Threading.Thread(AddressOf nt.StartProgram)
    nt.T.Start()

    'Add to the running list
    TList.Add(nt)
    Running.Add(spo.Name)
Catch ex As Exception
    EL.AddErr("Encountered an error while trying to start a thread for " & spo.Name & ". Error Details: " & ex.Message, EPath)
End Try
我从未输入catch块,因此不会出现完全错误,但我用于测试的程序的表单也不会显示。这里的上下文是
nt.StartProgram

Public Function StartProgram() As String
    '
    Dim ret As String = ""

    Try
        System.Diagnostics.Process.Start(prog)
    Catch ex As Exception
        ret = "Failed to start the program " & TaskName & " due to the following error: " & ex.Message
    End Try

    Return ret
End Function
我也从来没有进入过这里的捕捉区。作为最后一项检查,我硬编码创建和填充
nt
,而不是从内存中填充,然后尝试以编程方式启动它。这确实有效,代码如下

Private Sub TestForm_Load(sender As Object, e As EventArgs) Handles Me.Shown
    '
    'TM.MF = Me
    'TM.Main()    NOTE: this runs the code that I referred to above that doesn't seem to work

    Dim nt As New ExecThreadObj

    'Fill in the properties
    nt.Status = "Running"
    nt.TaskName = "Test"
    nt.prog = "C:\FullPathIWontShare.exe"
    nt.STime = Now
    nt.T = New System.Threading.Thread(AddressOf nt.StartProgram)
    nt.T.Start()
End Sub
为了充分披露,这里是TM.Main的全部内容

Public Sub Main()
    '
    Dim w As Integer = 3000 '500

    'Load the schedule
    If System.IO.File.Exists(SPath) Then
        'Create a backup copy
        System.IO.File.Copy(SPath, Replace(SPath, ".xml", "_backup.xml"))

        'Load the data
        X.FromXML(Sched, SPath, "Task Runner-Load", EPath)

        'Run the actual program
        Do While cont
            ThreadCheck()

            'Update the form
            MF.ListView1.Items.Clear()
            For Each t As ExecThreadObj In TList
                Dim temp(2) As String
                temp = {t.TaskName, t.STime}
                Dim NLI As New ListViewItem(temp)
                MF.ListView1.Items.Add(NLI)
            Next
            MF.Refresh()

            'Wait the proscribed period
            System.Threading.Thread.Sleep(w)
        Loop
    End If 'else there's nothing to run
End Sub
ThreadCheck具有启动和清理我将使用的线程的方法。问题出在StartThread()中,因此我也将说明这一点

Public Sub StartThreads()
    '
    Dim c As Boolean = True

    'Check the current thread count
    Dim TC As Integer = TList.Count

    'Can we start anything?
    If TC < 5 Then
        'Start threads for new tasks from the schedule (if there is space)
        For Each spo As ComObjLib.SchedProgObj In Sched
            If TList.Count < 5 Then
                If spo.NextRun.AddDays(spo.RT) <= Now And Not (Running.Contains(spo.Name)) Then
                    'The program should start, so do so
                    Try
                        Dim nt As New ExecThreadObj

                        'Fill in the properties
                        nt.Status = "Running"
                        nt.TaskName = spo.Name
                        nt.prog = spo.Prog
                        nt.STime = Now
                        nt.T = New System.Threading.Thread(AddressOf nt.StartProgram)

                        'Add to the running list
                        TList.Add(nt)
                        Running.Add(spo.Name)
                    Catch ex As Exception
                        EL.AddErr("Encountered an error while trying to start a thread for " & spo.Name & ". Error Details: " & ex.Message, EPath)
                    End Try
                End If 'else we don't need to do anything
            Else
                'do nothing for now, the task will have to wait
            End If
        Next
    End If

End Sub
Public子启动线程()
'
Dim c作为布尔值=真
'检查当前线程数
Dim TC As Integer=TList.Count
“我们能开始什么吗?
如果TC<5,则
'从计划中为新任务启动线程(如果有空间)
对于每个spo,在Sched中作为ComObjLib.SchedProgObj
如果t列表计数小于5,则
如果spo.NextRun.ADDDYS(spo.RT)