INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*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

(Download This Button Today!)
Member Feedback
"...Just to let you know...what a great site you have. I posted a pretty generic question yesterday and have had 8 responses already, anyway thanks again and keep up the good work..."
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
CODEImports System.Threading Then declare the variables you need
CODEPrivate m_Thread as Thread You need a method to start the thread, this example does it in the form load event
CODEPrivate 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
CODEPrivate 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)
CODEPrivate 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 |
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close