943,509 Members | Top Members by Rank

Ad:
Jul 4th, 2003
0

Using Delay or Pause, without the whole thing freezing?

Expand Post »
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdProduceWords_Click()
  2. txtWords.Text = "Hello"
  3. Sleep 300
  4. txtWords.Text = "Cookie"
  5. Sleep 300
  6. txtWords.Text = "Train"
  7. Sleep 300
  8. txtWords.Text = "Cat"

And if I were to click it, it would pause for a while, maybe freeze, then display "Cat".
Last edited by Comatose; Aug 14th, 2006 at 9:15 am.
Similar Threads
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 4th, 2003
0

Re: Using Delay or Pause, without the whole thing freezing?

Why not put the code into a timer?
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jul 4th, 2003
0

Re: Using Delay or Pause, without the whole thing freezing?

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q231/2/98.asp&NoWebContent=1

There is the answer to your problem. God bless Microsoft Knowledge Base.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Aug 1st, 2006
0

Re: Using Delay or Pause, without the whole thing freezing?

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 >>>

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. txtWords.Text = "Hello"
  2. Sleep 300
  3. txtWords.Text = "Cookie"
  4. txtWords.refresh
  5. Sleep 300
  6. txtWords.Text = "Train"
  7. txtWords.refresh
  8. Sleep 300
  9. txtWords.Text = "Cat"

Job Done ? :cheesy:



Quote originally posted by Mr Gates ...
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".
Last edited by Comatose; Aug 14th, 2006 at 9:16 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Paul finch is offline Offline
2 posts
since Aug 2006
Aug 14th, 2006
0

Re: Using Delay or Pause, without the whole thing freezing?

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):
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. for each XFrm in Forms
  2. unload XFrm
  3. next XFrm
  4.  
  5. ' /* Code Should Never Reach Here, But Just In Case */
  6. 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.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. TxtWords.Text = "Hello"
  2. DoEvents
  3. Sleep 300
  4. TxtWords.Text = "Cookie"
  5. DoEvents
  6. Sleep 300
  7. TxtWords.Text = "Train"
  8. DoEvents
  9. Sleep 300
  10. TxtWords.Text = "Cat"
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 27th, 2007
0

Re: Using Delay or Pause, without the whole thing freezing?

I agree with Comatose - this is a much better way.
Reputation Points: 10
Solved Threads: 3
Light Poster
mjwest10 is offline Offline
27 posts
since Feb 2007
Mar 28th, 2007
0

Re: Using Delay or Pause, without the whole thing freezing?

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
Reputation Points: 12
Solved Threads: 8
Junior Poster
davidcairns is offline Offline
114 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Extract MS Excel Data embedded in MS Word
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: creating a database in vb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC