我开始知道Thread.Suspend不是无限期暂停线程的好方法。如果有其他方法可以告诉我。
提前致谢。
答案 0 :(得分:1)
简短的vb示例
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
thrd.IsBackground = True
thrd.Start()
End Sub
Dim thrd As New Threading.Thread(AddressOf somethread)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'pause and resume
'first click pauses, second click resumes
reqpause.Set() 'set event
End Sub
Dim reqpause As New Threading.AutoResetEvent(False)
Private Sub somethread()
Do
'simulate work - your code here
Threading.Thread.Sleep(500)
Debug.WriteLine(DateTime.Now.ToLongTimeString)
'end simulate work
If reqpause.WaitOne(0) Then 'pause requested?
Debug.WriteLine("t-thrd paused")
reqpause.WaitOne() 'wait here for continuation
Debug.WriteLine("t-continue thrd")
End If
Loop
End Sub
答案 1 :(得分:0)
使用Suspend
和Resume
不是一个好主意。我不确定你为什么要永远等待,而是等待EventWaitHandle
等待。
private EventWaitHandle handle = new AutoResetEvent();
private void WorkerThread()
{
while(true)
{
handle.WaitOne();
}
}
//Forever is a long time
public void StopWaitingForever()
{
handle.Set();
}
答案 2 :(得分:0)