Problem with Threading.Thread.Sleep(3000)?

  • Thread starter Thread starter C
  • Start date Start date
C

C

This does not show me all the plots (all variables in List1). It shows
the last plot, and usually goes blank in between. What am I doing
wrong?

Private Sub cmdAll_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdAll.Click

Dim i As Short

For i = 1 To N ' N is a global public variable
Call cmdClear_Click(sender, e)
List1.SelectedIndex = i - 1
Call cmdPlot_Click(sender, e)
Threading.Thread.Sleep(3000)
Next

End Sub

I think I found the answer. I should have added Application.DoEvents()
in the For loop. Sleep(3000) is not the problem.

I had expected that Me.Invalidate() : g.Dispose() would force the
Paint process, so it would not be necessary to do anything else, but
why am I wrong?
 
Am 09.08.2010 21:22, schrieb C:
This does not show me all the plots (all variables in List1). It shows
the last plot, and usually goes blank in between. What am I doing
wrong?

Private Sub cmdAll_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmdAll.Click

Dim i As Short

For i = 1 To N ' N is a global public variable
Call cmdClear_Click(sender, e)
List1.SelectedIndex = i - 1
Call cmdPlot_Click(sender, e)
Threading.Thread.Sleep(3000)
Next

End Sub

I think I found the answer. I should have added Application.DoEvents()
in the For loop. Sleep(3000) is not the problem.

Well, 'Thread.Sleep' is not a good idea on a Windows Forms application's
main thread because it will lock the window. Is there any reason why
you are not using 'System.Windows.Forms.Timer' instead?
 
Am 09.08.2010 21:22, schrieb C:











Well, 'Thread.Sleep' is not a good idea on a Windows Forms application's
main thread because it will lock the window.  Is there any reason why
you are not using 'System.Windows.Forms.Timer' instead?

Didn't think of it. Secondly, I am a beginner..

What is meant by "locking the window"? That I cannot do anything else
on the window during that time? That is not a problem because this is
intended to quickly show a few plots in a few seconds. Then other
things could be done on the form. Thanks for your input.
 
Am 09.08.2010 21:22, schrieb C:
I should have added Application.DoEvents()
in the For loop.

No, the thread is already "doing the events" - if you wouldn't prevent
it from doing so by executing this long lasting loop.
Sleep(3000) is not the problem.

I had expected that Me.Invalidate() : g.Dispose() would force the
Paint process, so it would not be necessary to do anything else, but
why am I wrong?

Invalidate only invalidates the window, but as long as the loop is
running, the code doesn't return to the message loop that processes
WM_PAINT in order to paint the window. An instant refresh could be
forced by calling the Refresh method, but since Windows XP, this doesn't
work anymore because the thread is considered stalled and your window
is replaced by a ghost window. See 3rd paragraph here:
http://msdn.microsoft.com/en-us/library/ms644927(VS.85).aspx

So, solve it like Herfried suggested.
 
Back
Top