|
-
Oct 6th, 2011, 07:28 AM
#1
Thread Starter
Frenzied Member
Proper way to pause/resume a thread?
I have a thread running on my application that checks a database every so often to decide whether or not to update nodes in a treeview.
Each treeview node represents a document and I have an open button that opens the document for the user.
When the document is opened, I wanted to suspend the thread that refreshes the node list until the document is closed.
I have MyThread.Suspend and MyThread.Resume. But the document viewer is on another thread.
I created functions for suspending/resuming the thread in the application. I have delegates in the viewer to call those functions from another thread. But I'm still getting an Error. "Thread is not user-suspended. Cannot be resumed" or something of the like.
But I realize that Suspend and resume are obsolete functions, so what class or objects should I be looking into and if it didn't take up too much of your time, a small example as well?
Thanks,
Justin
-
Oct 6th, 2011, 04:33 PM
#2
PowerPoster
Re: Proper way to pause/resume a thread?
you do realise that trying to pause/resume threads is expensive?
you maybe better of using a timer here purely because you can control it easier and is more "efficient" than pausing/starting a thread which is expensive.
it would be helpful if you can post some code to take a look further otherwise solutions and explanations wont be given clearly and more accurately
-
Oct 6th, 2011, 05:18 PM
#3
Thread Starter
Frenzied Member
Re: Proper way to pause/resume a thread?
I figured out what the problem was. I created the thread and started it in the load code.
When I was refreshing the node list (re calling the load code) it was recreating the thread again. I recalled this function right before the thread.resume line was to be called.
At that point the thread as a new thread and was running, not suspended. That is why I got the error I did.
I'll post some code. I'm not sure how I would time it. I need the thread to sleep/suspend/pause up to an indefinite amount of time until the document is closed.
Only way I could think to do it. I'll post some code though for sure. I'm open to better ideas : ).
Thanks again,
Justin
-
Oct 6th, 2011, 10:44 PM
#4
Re: Proper way to pause/resume a thread?
Absolutely a Timer would be a better option here. You say that you need to check the database "every so often", which is exactly what a Timer is for. If you want to do the work in the background then it would make sense to use a Timers.Timer, which can raise its Elapsed event on a secondary thread, rather than a Windows.Forms.Timer. You can then simply call Stop and Start on the Timer.
-
Oct 7th, 2011, 02:39 AM
#5
Re: Proper way to pause/resume a thread?
Yea a Threading.Timer as jmcilhinney suggested would be better, or have a thread like you do with a loop like this:
vb Code:
Do
'DoStuffHere
'Now wait for a min
Threading.Thread.Sleep(60000)
Loop
If you use the above just kill the thread when you don't want to update anymore!
Kris
-
Oct 7th, 2011, 09:01 AM
#6
Re: Proper way to pause/resume a thread?
Here is some code using EventWaitHandle and Monitor that might give you some ideas.
Code:
Public Class Form1
Dim docOpen As New Threading.EventWaitHandle(False, Threading.EventResetMode.AutoReset)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Dim tr As New Threading.Thread(AddressOf foo) 'refresh list thread
tr.IsBackground = True
tr.Start()
Dim topn As New Threading.Thread(AddressOf bar) 'doc viewer thread
topn.IsBackground = True
topn.Start()
End Sub
Dim docIsOpen As New Object
Private Sub foo()
'this thread simulates refreshing the node list
Do
'try to acquire the lock
If Threading.Monitor.TryEnter(docIsOpen) Then
'got the lock
Debug.WriteLine(DateTime.Now.ToString) 'update list sim
Threading.Monitor.Exit(docIsOpen)
docOpen.WaitOne(1000) 'how long to wait between refreshes
Else
'doc must be open
docOpen.WaitOne() 'wait until doc processing is done
End If
Loop
End Sub
Private Sub bar()
Do
Threading.Monitor.Enter(docIsOpen) 'simulate open doc
Debug.WriteLine("doc open")
Threading.Thread.Sleep(4000)
Threading.Monitor.Exit(docIsOpen) 'done with the doc
docOpen.Set() 'signal that doc is closed
Threading.Thread.Sleep(3000) 'simulate a doc not open
Loop
End Sub
End Class
-
Oct 7th, 2011, 10:00 AM
#7
Re: Proper way to pause/resume a thread?
A timer is a good thing, but I see why you would also want to pause a thread. Any of the synchronization elements, such as the Mutex, or the newer items that DB suggested would be pretty lightweight solutions. The idea would be to have the thread try to acquire something shared between the threads before proceeding. When the UI thread wants to stop the background thread, it would acquire the synchronization element and hold it until it was done. The background thread would pause until the synchronization element was released. This should be pretty light, and would ensure that the background thread was paused in the same place every time. Ideally, that would be a place where the operation of the UI elements would not conflict with the thread.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|