[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Gracefully suspending / stopping a thread

Posted on 2009-07-09
4
Medium Priority
?
651 Views
Last Modified: 2012-05-07
Hi
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.

Thanks
0
Comment
Question by:peispud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24820464
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
        clk.SuspendThread()
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        clk.ResumeThread()
    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
            T.Start()
        End Sub
 
        Private Sub TickTock()
            While True
                System.Threading.Thread.Sleep(1000)
                SC.Post(New System.Threading.SendOrPostCallback(AddressOf TimeHack), DateTime.Now)
 
                If Suspend Then
                    MRE.Reset()
                    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()
            MRE.Set()
        End Sub
 
    End Class
 
End Class

Open in new window

0
 

Author Closing Comment

by:peispud
ID: 31601930
Thank you very much!!  I will have to think through your response,  but you have answered my question perfectly.  Much appreciated!!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24828450
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.  =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question