Вопрос

When I start a thread, the ThreadState always is "Unstarted" even if I do a "Thread.Abort()", my thread starts and finish the work good... I don't know why I get that always the same state.

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

UPDATE

This is the sub who calls the thread, and the problem is the "while" statament is not waiting,

PS: You can see a comment explanation at the middle of this sub:

 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

And here is the "mithread" thread:

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

Это было полезно?

Решение

Its not easy to work out what your problem is, but i can say for sure a While Loop and DoEvents is not the way forward at all.

Instead raise an event when the thread has done all its work, subscribe to the event, and when it is raise close the form (if 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

Or use a background worker which does all this, plus handles reporting progress and cancelation all for you.

Другие советы

Sounds like something is happening in mithread that is preventing the thread from fully starting. I ran similar code with an empty sub for mithread and I get the expected threadstate (Stopped then 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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top