Results 1 to 7 of 7

Thread: Proper way to pause/resume a thread?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  2. #2
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    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
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    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:
    1. Do
    2.     'DoStuffHere
    3.     'Now wait for a min
    4.     Threading.Thread.Sleep(60000)
    5. Loop

    If you use the above just kill the thread when you don't want to update anymore!

    Kris

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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
  •  



Click Here to Expand Forum to Full Width