Results 1 to 13 of 13

Thread: Pause Not Pausing

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Pause Not Pausing

    I have a function that i send it a int like 3, and the function will pause for 3 seconds then move on..
    However... i cant seem to get the pause to work correctly, it just shoots right through the function.

    Code:
        'Function To Control Pause / Wait Time Selected
        Private Sub Pause(ByVal seconds As Integer)
            Dim returnTime As Date = Now.AddSeconds(seconds)
            Do While Now < returnTime
                Application.DoEvents()
                Threading.Thread.Sleep(100)
            Loop
        End Sub
    
       Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            Try
              Pause(3)
              MsgBox("OK")
              Pause(3)
            Catch ex As Exception
               Pause(3)
               MsgBox("Error")
               Pause(3)
            End Try
        End Sub

  2. #2
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Pause Not Pausing

    maybe you can simply do


    Private Sub Pause(ByVal nSeconds As Integer)
    System.Threading.Thread.Sleep(nSeconds * 1000)
    Last edited by Guerrero; Oct 4th, 2010 at 02:41 PM.

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Pause Not Pausing

    I think when you do that, it freezes the app, i dont want it to freeze the app

  4. #4
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Pause Not Pausing

    Quote Originally Posted by joefox View Post
    I think when you do that, it freezes the app, i dont want it to freeze the app
    I use this technique in several sites and don't understand the freeze you comment.

    I think there is more freeze when doevents called each 100 milliseconds.
    maybe call doevents only one time each second.

    don't sure how to test the freezes you comment.

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Pause Not Pausing

    I just tried it, and it dosent give the desired results..

  6. #6
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Pause Not Pausing

    Quote Originally Posted by joefox View Post
    I just tried it, and it dosent give the desired results..
    what is "freeze"?
    some of blinking controls in the form?

    (btw I don't lile to using maybe-reserved-words as "seconds" as parameters)

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  7. #7

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Pause Not Pausing

    yes it acts like its trying to process something.
    before in my other apps it would make the function wait... then go not freezing the app

  8. #8
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Pause Not Pausing

    I've made a form with pause(9) and works fine.
    without using doevents,
    but using the full sentence (;-)
    System.Threading.Thread.Sleep(9 * 1000)

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  9. #9
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Pause Not Pausing

    Quote Originally Posted by joefox View Post
    yes it acts like its trying to process something.
    before in my other apps it would make the function wait... then go not freezing the app
    sure... your doevents....

    (I need a better signature) - Rec.Tools: DropBox, Everything, FileLocator, 7-Zip, Irfanview, PdfCreator, EditPad, NotePad++, Launchy

  10. #10
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Pause Not Pausing

    Not the way to go (i.e. do NOT use sleep like this to 'pause' your application).

    Depending on what you want to do, you can simply enable (start) a timer, after setting the desired interval (in milliseconds). When the timer tick event fires, you can continue processing, and disable the timer.

    Really, though, you need to rethink the necessity of a 'pause'. Windows is an event driven system, and as such, you need to respond to events through event driven programming. Timers are used to generate interval-based events for you to respond to.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  11. #11

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: Pause Not Pausing

    I have the pause working at home, but i have the full version of VS2010, here at work i have Express version of 2010 maybe thats why my "pause" style is not working?

  12. #12
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Pause Not Pausing

    Threading.Thread.Sleep doesn't freeze the application, just the thread. Of course, that doesn't make any practical difference if you are not using multiple threads. One way to pause something without freezing the app is to use a separate thread. An easy way to do that to use a System.Timers.Timer (not a Windows.Forms.Timer!). Its Elapsed event fires on a separate thread. Here's an example of a Delay sub that uses it:

    Code:
    Private WithEvents tim As New System.Timers.Timer With {.Enabled = False, .AutoReset = False}
    Private delayMessage As String
    
        Private Sub Delay(ByVal message As String, ByVal seconds As Single)
            tim.Interval = seconds * 1000
            delayMessage = message
            tim.Start()
        End Sub
    
        Private Sub tim_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
            MessageBox.Show(delayMessage)
        End Sub
    There are some limitations to what you can do in the Elapsed sub, though. A MessageBox works, and so does MsgBox. But you can't change anything directly in the UI such as setting a TextBox text, because you get a threading error. Instead, you could raise an event and handle it.

    BB

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

    Re: Pause Not Pausing

    What is the pause for? It looks like you are trying to "pause" the UI for 3 seconds, then show a message box, and after the user presses OK, "pause" the UI for 3 more seconds. As has been pointed out this is not something you should do, but without knowing your reasoning it is hard to provide help.
    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

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