Halt Sequence
-
I'm trying to find a safe way to put a delay of 4 seconds in my code in VB 6.0 (NOT .NET). This is the code I have: Public Sub Wait() i As Integer Halt = 4 Dim LTime LTime = Timer() While Timer() - LTime < Halt i = 1 Wend End Sub This works great except for one problem. If I am running a form, press a button that calls this function, and then close the form before this function is completed, the visual of the button is corrupted. Basically, when the draw window is called (IE with a form.show call), the button shows through to the background and saves that image at its interface. IE If I have a word doc behind it, close the form with wait in progress, and form.show the form, it will bleed through with the button with whatever text is in the box of the button. How do I fix this?:confused: Thank you, Red Sunday ----------------- http://www.zachcalvert.com
-
I'm trying to find a safe way to put a delay of 4 seconds in my code in VB 6.0 (NOT .NET). This is the code I have: Public Sub Wait() i As Integer Halt = 4 Dim LTime LTime = Timer() While Timer() - LTime < Halt i = 1 Wend End Sub This works great except for one problem. If I am running a form, press a button that calls this function, and then close the form before this function is completed, the visual of the button is corrupted. Basically, when the draw window is called (IE with a form.show call), the button shows through to the background and saves that image at its interface. IE If I have a word doc behind it, close the form with wait in progress, and form.show the form, it will bleed through with the button with whatever text is in the box of the button. How do I fix this?:confused: Thank you, Red Sunday ----------------- http://www.zachcalvert.com
Hi Red! I'm trying to find a safe way to put a delay of 4 seconds in my code in VB 6.0 (NOT .NET). First, don't use While...Wend to loop. Use a Do loop instead (see also How to Use Looping Structures in Visual Basic for Applications[^]). Second, don't use a loop at all. You can use Task-Manager to quickly find out that your loop will lead to 100% CPU usage. Use the API function Sleep instead.
Option Explicit
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Integer)Public Sub Wait()
Sleep 4000
End SubYou can find more about Sleep and an API function called SetWaitableTimer (which might solve your re-draw problem) in the article How To Use SetWaitableTimer With Visual Basic[^]. Best regards Dennis
-
I'm trying to find a safe way to put a delay of 4 seconds in my code in VB 6.0 (NOT .NET). This is the code I have: Public Sub Wait() i As Integer Halt = 4 Dim LTime LTime = Timer() While Timer() - LTime < Halt i = 1 Wend End Sub This works great except for one problem. If I am running a form, press a button that calls this function, and then close the form before this function is completed, the visual of the button is corrupted. Basically, when the draw window is called (IE with a form.show call), the button shows through to the background and saves that image at its interface. IE If I have a word doc behind it, close the form with wait in progress, and form.show the form, it will bleed through with the button with whatever text is in the box of the button. How do I fix this?:confused: Thank you, Red Sunday ----------------- http://www.zachcalvert.com
depending on what you are trying to do, there are two main options: 1. Causing the Code to completly halt can be done by using the Sleep method (i saw you got a got overview of it) or by looping like you did BUT - add a DoEvents() call inside the loop, this will allow the window to redraw this approach of halting is not very safe to begin with, especially in VB6 that runs EVERYTHING on a single thread. you might want to try these two approachs: 2. Timer based halt. Create a timer object, and set it to start timing you in the 'Wait' method handle the timer's "Timer" event, in the hanlding sub a. disable the timer b. call a method to continue the operation there are additional ways to achieve this, but i think this will get you going Fade (Amit BS)