How to: Doevent()

  • Thread starter Thread starter Jerry Haynes
  • Start date Start date
J

Jerry Haynes

I've looked in the July 2003 version of the MSDN Library and I'm not getting
much support on the Application.DoEvent method. I've written a small app to
get an idea of how it works and am having some difficulty. I simply want
the program to process the Ones() and Tens() subroutines at the same time
and when I press the "Stop" button, the program ends, but with the code
below, its only processing the Ones() subroutines and never gets to the
Tens(). Thanks to all that reply!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Button1.Text = "Start" Then
Button1.Text = "Stop"
Application.DoEvents()
Call Ones()
Call Tens()
Application.DoEvents()
Else
Application.Exit()
End If
End Sub

Private Sub Ones()
Do
Me.txtOnes.Text = Str(Val(Me.txtOnes.Text.ToString) + 1)
Me.txtOnes.Refresh()
Loop
End Sub

Private Sub Tens()
Do
Me.txtTens.Text = Str(Val(Me.txtTens.Text.ToString) + 10)
Me.txtTens.Refresh()
Loop
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
txtOnes.Text = 0
txtTens.Text = 0
End Sub
 
Hi,

I changed something, and I have removed those do loops, because they would
never end.
But to give you an idea.

Cor
 
Cor...the loop will stop when I press the "Stop" button. As you will see, I
had and application.exit command. But back to my original question, where
do I put the "application.doevent()" commands, so that both routines run
simultaneously?
 
Jerry Haynes said:
Cor...the loop will stop when I press the "Stop" button. As you will see, I
had and application.exit command. But back to my original question, where
do I put the "application.doevent()" commands, so that both routines run
simultaneously?

Jerry... the loop "stops" because an event interrupted it. Otherwise it is
an endless loop and it will never get to the Tens() routine so long as it
doesn't complete the Ones() routine and you have prevented it from ever
"finishing" because of the endless loop.

At the very least move your loop into the Button1_Click (remove them from
the Ones() and Tens() routines.

Do
Call Ones()
Call Tens()
Application.DoEvents()
Loop
 
Jerry Haynes said:
Cor...the loop will stop when I press the "Stop" button. As you will
see, I had and application.exit command. But back to my original
question, where do I put the "application.doevent()" commands, so
that both routines run simultaneously?

DoEvents does not make the routines run simultaneously. DoEvents processes
the messages in the message loop (like mouse and keyboard messages) for the
current thread. To have two routines run simultaneously, you must create two
threads.

see also:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconthreading.asp
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconThreadingInVisualBasic.asp
 
Armin Zingler said:
DoEvents does not make the routines run simultaneously. DoEvents
processes the messages in the message loop (like mouse and keyboard

Correction:
.... in the message _queue_ ...

:)
 
Hi Jerry,
Just as an addition to Tom and Armin

To add the example of Tom, you could do something as
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Select case Button1.text
case "Start"
Button1.Text="Tens"
Ones
case "Tens"
Button1.Text="Stop"
Tens
case "Stop"
me.close ' that is your application exit
end select
End sub

And then add that application.doevents like Tom did also in the Tens routine
but than with the do ....... loop.

Then fist the Ones do a loop till you push and then the tens do that loop

This just an addition two the two answers like I said.

Cor


Cor
 
Back
Top