0

我试图在线程暂停时关闭我的表单,但我收到了这个错误:Thread is suspended; attempting to abort.

如果线程正在运行并且我单击按钮关闭它很好。

这是我的代码:

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    t1.Abort()

    Me.Close()

End Sub

即使线程已挂起或未挂起,我也想关闭表单。

4

1 回答 1

0

这是休眠线程的模式以及如何“中止”它。请注意,.WaitOne 可以采用类似于 Thread.Sleep 的参数。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'start a thread
    If IsNothing(somethrd) Then
        somethrd = New Threading.Thread(AddressOf test)
        somethrd.IsBackground = True
        somethrd.Start()
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'stop
    stopsomethrd = True
    somethrdwait.Set()
    somethrd.Join()
    Debug.WriteLine("end")
End Sub

Dim somethrd As Threading.Thread
Dim somethrdwait As New Threading.AutoResetEvent(False)
Dim stopsomethrd As Boolean

Private Sub test()
    Do
        'your code
        somethrdwait.WaitOne() 'suspend thread
        If stopsomethrd Then Exit Do
    Loop
End Sub
于 2013-09-18T13:47:46.177 回答