ThreadState总是“Unstarted”

时间:2012-12-21 16:12:37

标签: vb.net multithreading visual-studio visual-studio-2012 thread-state

当我启动一个线程时,ThreadState总是“Unstarted”,即使我做了一个“Thread.Abort()”,我的线程开始并完成工作......我不知道为什么我总是这样做同一个州。

Dim thread_1 As System.Threading.Thread = New Threading.Thread(AddressOf mithread)
thread_1.Start()

System.Threading.Thread.Sleep(100)

While not thread_1.ThreadState = Threading.ThreadState.Running
    MsgBox(thread_1.ThreadState.ToString) ' "Unstarted"
    thread_1.Abort()
    MsgBox(thread_1.ThreadState.ToString) ' "Unstarted" again and again...
End While
  

更新

这是调用线程的子,问题是“while”statament没有等待,

PS:您可以在本子中间看到评论说明:

 public sub...
        ...
        If Not playerargs = Nothing Then
            If randomize.Checked = True Then
                Dim thread_1 As System.Threading.Thread = New Threading.Thread(AddressOf mithread)
                thread_1.Start()

                System.Threading.Thread.Sleep(50)
                While thread_1.ThreadState = Threading.ThreadState.Running
                    Windows.Forms.Application.DoEvents()
                End While

            Else
                progresslabel.Text = "All files added..."
            End If

            ' HERE IS THE PROBLEM, IF I CHECK "AUTOCLOSE" CHECKBOX THEN,
            ' THE FORM ALWAYS TRY TO CLOSE BEFORE THREAD IS COMPLETED:
            ' -----------------------------------------
            If autoclose.Checked = True Then Me.Close()
            '------------------------------------------
        Else
        ...
 End Sub

这是“mithread”主题:

Public Sub mithread()

Dim Str As String
Dim Pattern As String = ControlChars.Quote
Dim ArgsArray() As String
Str = Replace(playerargs, " " & ControlChars.Quote, "")
ArgsArray = Split(Str, Pattern)
Using objWriter As New System.IO.StreamWriter(Temp_file, False, System.Text.Encoding.UTF8)
    Dim n As Integer = 0
    Dim count As Integer = 0
    Dim foldercount As Integer = -1

    For Each folder In ArgsArray
        foldercount += 1
        If foldercount > 1 Then
            InvokeControl(ProgBarPlus1, Sub(x) x.Max = foldercount)
        End If
    Next

    If foldercount = 1 Then
        For Each folder In ArgsArray
            If Not folder = Nothing Then
                Dim di As New IO.DirectoryInfo(folder)
                Dim files As IO.FileInfo() = di.GetFiles("*")
                Dim file As IO.FileInfo
                InvokeControl(ProgBarPlus1, Sub(x) x.Max = files.Count)
                For Each file In files
                    n += 1
                    CheckPrimeNumber(n)
                    count += 1
                    If file.Extension.ToLower = ".lnk" Then
                        Dim ShotcutTarget As String = Shortcut.ResolveShortcut((file.FullName).ToString())
                        objWriter.Write(ShotcutTarget & vbCrLf)
                    Else
                        objWriter.Write(file.FullName & vbCrLf)
                    End If
                Next
            End If
        Next
    ElseIf foldercount > 1 Then
        For Each folder In ArgsArray
            If Not folder = Nothing Then
                Dim di As New IO.DirectoryInfo(folder)
                Dim files As IO.FileInfo() = di.GetFiles("*")
                Dim file As IO.FileInfo
                InvokeControl(ProgBarPlus1, Sub(x) x.Value += 1)
                For Each file In files
                    If file.Extension.ToLower = ".lnk" Then
                        Dim ShotcutTarget As String = Shortcut.ResolveShortcut((file.FullName).ToString())
                        objWriter.Write(ShotcutTarget & vbCrLf)
                    Else
                        objWriter.Write(file.FullName & vbCrLf)
                    End If
                Next
            End If
        Next
    End If
End Using

If Not thread_1.ThreadState = Threading.ThreadState.AbortRequested Then
    MsgBox(thread_1.ThreadState.ToString)
    Randomize_a_file.RandomizeFile(Temp_file)
    InvokeControl(ProgBarPlus1, Sub(x) x.Value = 0)
    '  Process.Start(userSelectedPlayerFilePath, ControlChars.Quote & Temp_file.ToString() & ControlChars.Quote)
    InvokeControl(progresslabel, Sub(x) x.Text = "All files launched...")
End If

End Sub

2 个答案:

答案 0 :(得分:1)

听起来mithread正在发生阻止线程完全启动的事情。我为mithread运行了一个带有空子的类似代码,然后我得到了预期的threadstate(Stopped然后Aborted)。

    Sub Main()

    Dim thread_1 As System.Threading.Thread = New Threading.Thread(AddressOf mithread)
    thread_1.Start()

    System.Threading.Thread.Sleep(100)

    While Not thread_1.ThreadState = Threading.ThreadState.Running
        Console.WriteLine(thread_1.ThreadState.ToString)
        thread_1.Abort()
        Console.WriteLine(thread_1.ThreadState.ToString)
        If thread_1.ThreadState = Threading.ThreadState.Aborted Then Exit While
    End While

End Sub

Sub mithread()
End Sub

答案 1 :(得分:1)

解决你的问题并不容易,但我可以肯定地说,While Loop和DoEvents根本不是前进的方法。

相反,当线程完成所有工作,订阅事件以及何时关闭表单(如果autoclose = true)时引发事件:

Public Class Form1
Public Event threadCompleted()
Public Sub New()
    InitializeComponent()
    AddHandler threadCompleted, AddressOf Me.Thread_Completed
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim t1 As New Threading.Thread(AddressOf mithread)
    t1.Start()
End Sub

Public Sub mithread()
    'simulate some work:
    Threading.Thread.Sleep(3000)
    'then raise the event when done
    RaiseEvent threadCompleted()
End Sub

Public Delegate Sub Thread_CompletedDelegate()
Private Sub Thread_Completed()
    If Me.InvokeRequired Then
        Me.BeginInvoke(New Thread_CompletedDelegate(AddressOf Thread_Completed))
    Else
        If autoclose.Checked = True Then
            Me.Close()
        End If
    End If
End Sub
End Class

或者使用后台工作人员完成所有这些操作,并为您处理报告进度和取消。