2011年1月13日 星期四

pause and restart a thread

Imports System.Threading

Private m_Thread as Thread

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Make a new counter object.
Dim new_counter As New Counter(Me)

' Make the thread to run the object's Run method.
m_Thread = New Thread(AddressOf new_counter.Run)

' Make this a background thread so it automatically
' ends when the form's thread ends.
m_Thread.IsBackground = True
m_thread.Start
Button1.Enabled = False
Button2.Enabled = True
End Sub

Private Sub Button1_Click(ByVal sender as Object, ByVal e As System.EventArgs) Handles Button1.Click
If (m_Thread.ThreadState And ThreadState.Unstarted) <> _
0 Then
' The thread has never been started. Start it.
m_Thread.Start()
Else
' The thread is paused. Resume it.
m_Thread.Resume()
End If

Button1.Enabled = True
Button2.Enabled = False
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Suspend the thread.
Debug.WriteLine("Suspending thread")
m_Thread.Suspend()

Button1.Enabled = False
Button2.Enabled = True
End Sub

沒有留言:

張貼留言