Results 1 to 6 of 6

Thread: new pause funchion

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    new pause funchion

    Code:
    Private Sub TimeOut(HowLong)
        Dim TheBeginning
        Dim NoFreeze As Integer
        TheBeginning = Timer
    
    
        Do
            If Timer - TheBeginning >= HowLong Then Exit Sub
            NoFreeze% = DoEvents()
        Loop
    End Sub

    use
    TimeOut 4

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: new pause funchion

    Would this require you to actually place a timer on the form?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: new pause funchion

    I appreciate what you're trying to do but that is a terrible way to do it and noone should ever use code like that. What you have there is called a "busy wait loop". What that means is that, in order to get your app to do nothing, you have it hitting the system at 100%. While that loop is running your processor will be running at 100% capacity. That's bad, bad, bad! If you want your app to do nothing then it should actually do nothing.

    You can call Thread.Sleep to pause and do nothing but that is a problem because it means that the UI will lock up for that period. You're obviously trying to provide a way to avoid that lock up but it is misguided. If you want to pause without freezing the UI then you simply do not call a synchronous method. For one off cases, you can simply use a Timer. The code you want performed before the pause goes where it usually would and then you call Start on the Timer to pause. That should be the end of that method and the code you want executed after the pause would go in the Tick event handler of the Timer, after a call to Stop. Where using your method might look like this:
    Code:
    Private Sub DoWork()
        DoSomething()
        TimeOut(5000)
        DoSomethingElse()
    End Sub
    using a Timer would look like this:
    Code:
    Private Sub DoWork()
        DoSomething()
        Timer1.Start()
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        DoSomethingElse()
    End Sub
    It may take a little more code but you should never use a hack to avoid writing a little bit of code. If you want something a bit more general purpose then you could easily create a class that provided the functionality.

  4. #4
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: new pause funchion

    Quote Originally Posted by jmcilhinney View Post
    I appreciate what you're trying to do but that is a terrible way to do it and noone should ever use code like that. What you have there is called a "busy wait loop". What that means is that, in order to get your app to do nothing, you have it hitting the system at 100%. While that loop is running your processor will be running at 100% capacity. That's bad, bad, bad! If you want your app to do nothing then it should actually do nothing.

    You can call Thread.Sleep to pause and do nothing but that is a problem because it means that the UI will lock up for that period. You're obviously trying to provide a way to avoid that lock up but it is misguided. If you want to pause without freezing the UI then you simply do not call a synchronous method. For one off cases, you can simply use a Timer. The code you want performed before the pause goes where it usually would and then you call Start on the Timer to pause. That should be the end of that method and the code you want executed after the pause would go in the Tick event handler of the Timer, after a call to Stop. Where using your method might look like this:
    Code:
    Private Sub DoWork()
        DoSomething()
        TimeOut(5000)
        DoSomethingElse()
    End Sub
    using a Timer would look like this:
    Code:
    Private Sub DoWork()
        DoSomething()
        Timer1.Start()
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        DoSomethingElse()
    End Sub
    It may take a little more code but you should never use a hack to avoid writing a little bit of code. If you want something a bit more general purpose then you could easily create a class that provided the functionality.
    Original post looks like VB6.

    But in vbNet, couldn't you use System.Threading.Thread.Sleep(timeout)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: new pause funchion

    Quote Originally Posted by Zach_VB6 View Post
    Original post looks like VB6.

    But in vbNet, couldn't you use System.Threading.Thread.Sleep(timeout)
    Thread.Sleep will create a genuine pause but the issue there is that it blocks the current thread. If you call Thread.Sleep on the main thread of a GUI app then you freeze the UI. The fact that a variable in the original code is called 'NoFeeze', this code is intended to avoid that. You might like to check this out:

    http://www.vbforums.com/showthread.p...e-to-Your-Code

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: new pause funchion

    Moved to the VB6 CodeBank.

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