By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Problem with setting up time delay in a For-Next loop

100+
P: 106
I am encountering difficulty in setting up a time delay in my speed reader software.

Basically, the user will load a text file and the software will take each word into an array.
on pressing the start button, the words will be displayed one at a time at a rate defined by the reading speed suited to the reader.

using the debugging techniques, (breakpoint / F10), but Without any of the delay code, system works fine.

the moment, I insert the delay code, words are not displayed at all. I do not know why.

This code works fine, but the msgbox needs to be replaced with a time delay.

Expand|Select|Wrap|Line Numbers
  1. For words = 1 To CDbl(txtNoofWords.Text)
  2. txtstdTextBox.Text = m_WordsList(words)
  3. ReadingDelay = (60 / CDbl(txtWPM.Text)) * 1000 'time in millseconds
  4. MsgBox("Pause")
  5. Next
  6. MsgBox("End of Chapter")
  7.  

I have used the Thread.sleep method. yet again, the words are not displayed, but the loop completes successfully.
Expand|Select|Wrap|Line Numbers
  1. Dim ReadingDelay As Double = 0
  2. ReadingDelay = (60 / CDbl(txtWPM.Text)) * 1000 'time in millseconds
  3. For words = 1 To CDbl(txtNoofWords.Text)
  4. txtstdTextBox.Text = m_WordsList(words)
  5. Threading.Thread.Sleep(ReadingDelay)
  6. Next
  7. MsgBox("End of Chapter")
  8.  

I have tried to this code but to no joy.
Expand|Select|Wrap|Line Numbers
  1. Dim ReadingDelay As Double = 0
  2. Dim dateFormat As String = "MM/dd/yyyy hh:mm:ss.fffffff"
  3. ReadingDelay = (60 / CDbl(txtWPM.Text)) * 1000 'time in millseconds
  4. For words = 1 To CDbl(txtNoofWords.Text)
  5. txtstdTextBox.Text = m_WordsList(words)
  6. Dim date2 As Date = Now.AddMilliseconds(2000).ToString(dateFormat)
  7. Do While date2 > Now.ToString(dateFormat)
  8. Loop
  9. Next
  10. MsgBox("End of Chapter")
  11.  
Aug 9 '14 #1

✓ answered by ambusy

This works: doEvents refreshes the window:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim m_wordsList() As String = {"q", "b", "c"}
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim ReadingDelay As Double = 0
  5.         ReadingDelay = (60 / CDbl(txtWPM.Text)) * 1000 'time in millseconds
  6.         For words = 1 To m_wordsList.Count
  7.             txtstdTextBox.Text = m_wordsList(words - 1)
  8.             Application.DoEvents()
  9.             Threading.Thread.Sleep(ReadingDelay)
  10.         Next
  11.         MsgBox("End of Chapter")
  12.  
  13.     End Sub
  14. End Class

Share this Question
Share on Google+
1 Reply


P: 5
This works: doEvents refreshes the window:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim m_wordsList() As String = {"q", "b", "c"}
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim ReadingDelay As Double = 0
  5.         ReadingDelay = (60 / CDbl(txtWPM.Text)) * 1000 'time in millseconds
  6.         For words = 1 To m_wordsList.Count
  7.             txtstdTextBox.Text = m_wordsList(words - 1)
  8.             Application.DoEvents()
  9.             Threading.Thread.Sleep(ReadingDelay)
  10.         Next
  11.         MsgBox("End of Chapter")
  12.  
  13.     End Sub
  14. End Class
Aug 18 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.