• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

Gracefully suspending / stopping a thread

I have just now learning about threads.  
Here is what I want do to......   I wish to create a thread and "Start" it.

I want to be able to suspend it (and later use it again) in my application.
I also wold like to destroy it as I close my application.

Of course,  I would like to do this gracefully.

I would appreciate a short demo.

  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can't really suspend a thread at any given time...you have to pick discrete points in the thread loop/code where YOU check some kind of flag and then put the thread into a waiting state.  One way to accomplish this is with a ManualResetEvent.

Here is an example using a simple "clock thread"...add a Label and two Buttons to a blank form:
Public Class Form1
    Private WithEvents clk As Clock
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clk = New Clock
    End Sub
    Private Sub clk_CurrentTime(ByVal dt As Date) Handles clk.CurrentTime
        Label1.Text = dt.ToString
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub
    Private Class Clock
        Private Suspend As Boolean = False
        Private T As System.Threading.Thread
        Public Event CurrentTime(ByVal dt As DateTime)
        Private SC As System.Threading.SynchronizationContext
        Private MRE As New System.Threading.ManualResetEvent(False)
        Public Sub New()
            SC = System.Windows.Forms.WindowsFormsSynchronizationContext.Current
            T = New System.Threading.Thread(AddressOf TickTock)
            T.IsBackground = True ' <-- allow thread to be killed automatically when app closes
        End Sub
        Private Sub TickTock()
            While True
                SC.Post(New System.Threading.SendOrPostCallback(AddressOf TimeHack), DateTime.Now)
                If Suspend Then
                    MRE.WaitOne() ' will STOP and WAIT here until MRE is signaled w/ the Set() method
                    Suspend = False
                End If
            End While
        End Sub
        Private Sub TimeHack(ByVal state As Object)
            RaiseEvent CurrentTime(state)
        End Sub
        Public Sub SuspendThread()
            Suspend = True
        End Sub
        Public Sub ResumeThread()
        End Sub
    End Class
End Class

Open in new window

peispudAuthor Commented:
Thank you very much!!  I will have to think through your response,  but you have answered my question perfectly.  Much appreciated!!
Mike TomlinsonMiddle School Assistant TeacherCommented:
Glad it was helpful.  I understand that's a lot to take in...feel free to ask any questions about it and I'll do my best to answer them.  =)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now