Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Pausing a thread

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] [2005] Pausing a thread

    I did a search on this site and all results said to use Thread.Suspend. Now in vs2005 Thread.Suspend is deprecated/obsolete. I checked MSDN and it said that the method will be removed from the next version. Is there a different way to pause a thread?

    I have tried to use sleep in a sub like this:

    Code:
        Sub SuspendThread(ByVal t As Threading.Thread)
            While bPaused
                t.Sleep(100)
            End While
        End Sub
    But, t.Sleep is underlined green. When I check the tooltip it says, Access of a shared member ... qualifying expression will not be evaluated.

    I shortened the message. It wants me to convert t.Sleep to Thread.Sleep. This seems like it may cause the wrong thread to sleep on accident.

    Any ideas?
    Prefix has no suffix, but suffix has a prefix.

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

    Re: [2005] Pausing a thread

    The Thread.Sleep method ALWAYS sleeps the current thread. There is no way to sleep a thread other than the current thread. If that's the end result you need then you must set a variable that will be read by the target thread that will then sleep itself.

    The reason that the Suspend method is being removed is because it makes it too easy to cause deadlock. By calling Sleep repeatedly you avoid that possibility. If you explain why you fell you need to suspend a thread we may be able to suggest a better solution.
    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

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Pausing a thread

    I just need to pause and resume a thread programmatically. I will try to use the sleep method.
    Prefix has no suffix, but suffix has a prefix.

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

    Re: [2005] Pausing a thread

    Quote Originally Posted by Troy Lundin
    I just need to pause and resume a thread programmatically. I will try to use the sleep method.
    That's an explanation of WHAT, not WHY. There have been many occasions where I've answered someone's question, only to discover that they shouldn't have been asking how to do it in the first place because doing something else entirely. If people would provide background to their questions then this sort of situation could be avoided and time could be saved on both sides. You thought you needed to use Thread.Suspend but that's been deprecated for reasons unbeknown to you. That suggests to me that you may not be doing things the best way. I guess we'll never know.
    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

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [RESOLVED] [2005] Pausing a thread

    Thanks.
    Prefix has no suffix, but suffix has a prefix.

  6. #6
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Re: [RESOLVED] [2005] Pausing a thread

    Well I'm in this same boat I think. I have a read thread that while the channel is open, it keeps listening. When I close the channel, I call mythread.abort(). But when I reopen, and send a message, it gets the message ok, but the first one always hits my Catch ex giving an error about "Thread was tryng to abort". But then it's fine after that. The odd thing, is that I always call NEW when i make the readthread object, so I would think it would get a completely new thread.

    If i use mythread.suspend, I don't get the error on re-connect.

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