Sleep command problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MACH4
    New Member
    • May 2008
    • 4

    Sleep command problem

    Hi all,
    First post!
    Learning vb.net

    the following is the button handler code:

    TextBox1.text = "Sleeping"
    Thread.Sleep(50 0)
    TextBox1.text = "Done!"

    Why is the first line being ignored?

    TextBox stays empty until after Sleep() then shows "Done" message...

    Thanks for any help!
  • misza
    New Member
    • Feb 2008
    • 13

    #2
    Originally posted by MACH4
    Hi all,
    First post!
    Learning vb.net

    the following is the button handler code:

    TextBox1.text = "Sleeping"
    Thread.Sleep(50 0)
    TextBox1.text = "Done!"

    Why is the first line being ignored?

    TextBox stays empty until after Sleep() then shows "Done" message...

    Thanks for any help!
    Hi,

    Try calling Application.DoE vents before the sleep line.

    The answer to "why": for my (limited still) knowledge, it would have something to do with form events (paint, etc) - the real time they're being executed; pls someone elaborate on this;)

    hope this helps
    M.

    Comment

    • MACH4
      New Member
      • May 2008
      • 4

      #3
      Hi M,

      Thanks for replying!
      Yes your additional line does work thanks!

      Must admit I haven't used the Sleep command much before and my default language is Assembler, which prompted me to check it out further in that language.

      It seems that Do.Events (in this particular case) is equivalent to the UpdateWindow api function call. Using InvalidateRect to force a re-paint didn't work...

      MACH4

      Comment

      • MACH4
        New Member
        • May 2008
        • 4

        #4
        Hi M,

        Just to qualify my last message statement, I've just concocted the following code using the win32 api "UpdateWindow". ..

        Declare Auto Function UpdateWindow Lib "user32"(By Val hwnd As IntPtr) As IntPtr


        ' Button handler code

        Dim ptr As IntPtr = Handle

        TextBox1.Text = "Waiting"
        UpdateWindow(pt r)
        Thread.Sleep(50 0)
        TextBox1.Text = "Done!"

        As you can see, its a bit clumbersome for a VB.NET application but seems explain (at least partially) what Do.Events is performing!

        Thanks!

        MACH4

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          do events just tells the program to process the windows messages before continuing execution.
          Telling your textbox to .Update() would accomplish the text being updated as well.

          Comment

          • MACH4
            New Member
            • May 2008
            • 4

            #6
            Hi Plater,

            Yes, thanks or the info! I'm guilty of looking too deep!

            One thing that is becoming obvious with .NET is that if some code is more than 5 lines long, then there must exist an easier way! lol

            MACH4

            Comment

            Working...