-1

当我启动一个线程时,即使我执行“Thread.Abort()”,ThreadState 始终为“未启动”,我的线程启动并完成工作良好......我不知道为什么我总是得到相同的状态.

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”状态没有等待,

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

结束子

4

2 回答 2

1

听起来 mithread 中发生了一些事情,阻止了线程完全启动。我用 mithread 的空子运行了类似的代码,我得到了预期的线程状态(停止然后中止)。

    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
于 2012-12-21T16:51:37.593 回答
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

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

于 2012-12-21T18:08:04.533 回答