Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pause and restart a thread

How-to

How to pause and restart a thread

by  PsychoCoder  Posted    (Edited  )
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:
[color blue]Imports System.Threading[/color]

Then declare the variables you need
Code:
[color blue]Private m_Thread as Thread[/color]

[color blue]You need a method to start the thread, this example does it in the form load event[/color]
Code:
[color blue]Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load[/color]
    [color green]' Make a new counter object.[/color]
    [color blue]Dim new_counter As New Counter(Me)[/color]

    [color green]' Make the thread to run the object's Run method.[/color]
    [color blue]m_Thread = New Thread(AddressOf new_counter.Run)[/color]

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

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:
[color blue]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[/color]
        [color green]' The thread has never been started. Start it.[/color]
       [color blue] m_Thread.Start()
    Else[/color]
        [color green]' The thread is paused. Resume it.[/color]
        [color blue]m_Thread.Resume()
    End If

    Button1.Enabled = True
    Button2.Enabled = False
End Sub[/color]

Then finally, the procedure to pause the thread (once again in a button click event)
Code:
[color blue]Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click[/color]
    [color green]' Suspend the thread.[/color]
    [color blue]Debug.WriteLine("Suspending thread")
    m_Thread.Suspend()

    Button1.Enabled = False
    Button2.Enabled = True
End Sub[/color]


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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top