954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using Delay or Pause, without the whole thing freezing?

I'm still using VB6 unfortunetly, but anyway, how would I go about implementing this?
Say I wanted to do something so when you click a button the text in a textbox changes but has a 200-300 millisecond delay so it looks as if the words are changing really fast. But whenever I try to do it, I click the button to test it, and it waits like 4 seconds(freezes), then displays the last word I put for it to display. How do I fix that?

Here's an example:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdProduceWords_Click()
txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
Sleep 300
txtWords.Text = "Train"
Sleep 300
txtWords.Text = "Cat"


And if I were to click it, it would pause for a while, maybe freeze, then display "Cat".

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

Why not put the code into a timer?

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 
Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

Your freezing really isnt the issue, you code seems fine. The problem is that the app doesnt have enough time to refresh the text boxes.

So the quick and simple solution would be >>>

txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
txtWords.refresh
Sleep 300
txtWords.Text = "Train"
txtWords.refresh
Sleep 300
txtWords.Text = "Cat"


Job Done ? :cheesy:I'm still using VB6 unfortunetly, but anyway, how would I go about implementing this?
Say I wanted to do something so when you click a button the text in a textbox changes but has a 200-300 millisecond delay so it looks as if the words are changing really fast. But whenever I try to do it, I click the button to test it, and it waits like 4 seconds(freezes), then displays the last word I put for it to display. How do I fix that?

Here's an example:

-------
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
-------
Private Sub cmdProduceWords_Click()
txtWords.Text = "Hello"
Sleep 300
txtWords.Text = "Cookie"
Sleep 300
txtWords.Text = "Train"
Sleep 300
txtWords.Text = "Cat"
-------


And if I were to click it, it would pause for a while, maybe freeze, then display "Cat".

Paul finch
Newbie Poster
2 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Right, Paul had the right answer. You'll also find (which may be a better solution, since the .refresh method doesn't give windows a chance to update anything other than the textbox) that the doevents function will also work. Doevents, unlike .refresh, gives your app a chance to process events passed by the user. An example of this, is to stick another button on that form, for "quit", and have it have some unload and end code (like so):

for each XFrm in Forms
     unload XFrm
next XFrm

' /* Code Should Never Reach Here, But Just In Case */
End

Then, click your button to make the words change, and right after, click the Quit button. With the .refresh method, you'll see that it takes a lag time before ending the application. If you use the Doevents function, your application will respond more promptly to device control. The example I have here isn't as extreme as it could be (say, by having another textbox in which you type in at the same time), but it's enough to show you the difference between .refresh and doevents.

TxtWords.Text = "Hello"
DoEvents
Sleep 300
TxtWords.Text = "Cookie"
DoEvents
Sleep 300
TxtWords.Text = "Train"
DoEvents
Sleep 300
TxtWords.Text = "Cat"
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

I agree with Comatose - this is a much better way.

mjwest10
Light Poster
27 posts since Feb 2007
Reputation Points: 10
Solved Threads: 3
 

Actually I would disagree with this, refresh is the appropriate command to use here. DoEvents is for allowing further user interaction whilst code is executing, not for refreshing controls on a form.

See here for a fuller explanation
http://www.devx.com/vb2themax/Tip/18646


Regards

D

davidcairns
Junior Poster
114 posts since Feb 2007
Reputation Points: 12
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You