INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Congratulations on a brilliant idea and a great site..."

Geography

Where in the world do Tek-Tips members come from?

Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ

How-to

How to pause and restart a thread
Posted: 22 Feb 07

In the application I'm currently working on I needed not only to use threading (some real intense processes) but I needed to be able to pause said thread then restart it when a condition was met.

After some testing, trial & error, and researching I found a solution to my quandry; To stop and start a thread (this example uses the click of a button but can be implemented in several ways). Of course you have to have your imports

CODE

Imports System.Threading

Then declare the variables you need

CODE

Private m_Thread as Thread

You need a method to start the thread, this example does it in the form load event

CODE

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

The following procedure checks to see if the thread was ever started (Unstarted Property) and that it isnt running (ThreadState Property) then determines whether to start or resume the thread

CODE

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

Then finally, the procedure to pause the thread (once again in a button click event)

CODE

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


Thats it, check to see if the thread has either been started before or if its running, if both are true then start the thread, otherwise resume the thread.

Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend

My Archive