Skip to content

Commit 36307a1

Browse files
CopilotBillWagner
andcommitted
Replace problematic DoEvents example with console-based timer example
Co-authored-by: BillWagner <[email protected]>
1 parent c953bfc commit 36307a1

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

docs/visual-basic/language-reference/statements/raiseevent-statement.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,19 @@ RaiseEvent eventname[( argumentlist )]
5454

5555
The class that raises an event is the event source, and the methods that process the event are the event handlers. An event source can have multiple handlers for the events it generates. When the class raises the event, that event is raised on every class that has elected to handle events for that instance of the object.
5656

57-
The example also uses a form (`Form1`) with a button (`Button1`) and a text box (`TextBox1`). When you click the button, the first text box displays a countdown from 10 to 0 seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done".
57+
The example demonstrates a simple timer that counts down from 10 to 0 seconds and displays the progress to the console. When the countdown finishes, it displays "Done".
5858

59-
The code for `Form1` specifies the initial and terminal states of the form. It also contains the code executed when events are raised.
60-
61-
To use this example, open a new Windows Application project, add a button named `Button1` and a text box named `TextBox1` to the main form, named `Form1`. Then right-click the form and click **View Code** to open the Code Editor.
62-
63-
Add a `WithEvents` variable to the declarations section of the `Form1` class.
59+
Add a `WithEvents` variable to your class declarations.
6460

6561
[!code-vb[VbVbalrEvents#14](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#14)]
6662

6763
## Example 2
6864

69-
Add the following code to the code for `Form1`. Replace any duplicate procedures that may exist, such as `Form_Load`, or `Button_Click`.
65+
Add the following code to implement the event handlers and timer logic. This example shows how to use the `RaiseEvent` statement to notify event handlers when the timer updates or completes.
7066

7167
[!code-vb[VbVbalrEvents#15](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#15)]
7268

73-
Press F5 to run the preceding example, and click the button labeled **Start**. The first text box starts to count down the seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done".
74-
75-
> [!NOTE]
76-
> The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. To allow the form to handle the events directly, you can use multithreading. For more information, see [Managed Threading](../../../standard/threading/managed-threading-basics.md).
69+
When you run the preceding example, it starts counting down the seconds from 10 to 0, displaying the progress to the console. When the full time (10 seconds) has elapsed, it displays "Done".
7770

7871
## See also
7972

samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -163,45 +163,36 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042
163163
Private WithEvents TextBox1 As New TextBox
164164

165165
' <snippet14>
166-
Private WithEvents mText As TimerState
166+
Private WithEvents mTimer As TimerState
167167
' </snippet14>
168168

169169
' <snippet15>
170-
Private Sub Form1_Load() Handles MyBase.Load
171-
Button1.Text = "Start"
172-
mText = New TimerState
173-
End Sub
174-
Private Sub Button1_Click() Handles Button1.Click
175-
mText.StartCountdown(10.0, 0.1)
176-
End Sub
170+
Private WithEvents mTimer As TimerState
177171

178-
Private Sub mText_ChangeText() Handles mText.Finished
179-
TextBox1.Text = "Done"
172+
Private Sub StartCountdownExample()
173+
Console.WriteLine("Starting countdown from 10 seconds...")
174+
mTimer = New TimerState()
175+
mTimer.StartCountdown(10.0, 1.0)
180176
End Sub
181177

182-
Private Sub mText_UpdateTime(ByVal Countdown As Double
183-
) Handles mText.UpdateTime
178+
Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime
179+
Console.WriteLine($"Time remaining: {Countdown:0.0} seconds")
180+
End Sub
184181

185-
TextBox1.Text = Format(Countdown, "##0.0")
186-
' Use DoEvents to allow the display to refresh.
187-
My.Application.DoEvents()
182+
Private Sub mTimer_Finished() Handles mTimer.Finished
183+
Console.WriteLine("Done")
188184
End Sub
189185

190186
Class TimerState
191187
Public Event UpdateTime(ByVal Countdown As Double)
192188
Public Event Finished()
193189
Public Sub StartCountdown(ByVal Duration As Double,
194190
ByVal Increment As Double)
195-
Dim Start As Double = DateAndTime.Timer
196-
Dim ElapsedTime As Double = 0
197-
198191
Dim SoFar As Double = 0
199-
Do While ElapsedTime < Duration
200-
If ElapsedTime > SoFar + Increment Then
201-
SoFar += Increment
202-
RaiseEvent UpdateTime(Duration - SoFar)
203-
End If
204-
ElapsedTime = DateAndTime.Timer - Start
192+
Do While SoFar < Duration
193+
System.Threading.Thread.Sleep(CInt(Increment * 1000)) ' Sleep for increment seconds
194+
SoFar += Increment
195+
RaiseEvent UpdateTime(Duration - SoFar)
205196
Loop
206197
RaiseEvent Finished()
207198
End Sub

0 commit comments

Comments
 (0)