Skip to content

Commit 9a6e16d

Browse files
CopilotBillWagner
andauthored
Replace problematic DoEvents example with console-based RaiseEvent demonstration (dotnet#48538)
* Initial plan * Replace problematic DoEvents example with console-based timer example Co-authored-by: BillWagner <[email protected]> * Fix VB.NET syntax errors in RaiseEvent example - remove C# string interpolation, move to standalone classes, use proper VB syntax Co-authored-by: BillWagner <[email protected]> * Update docs/visual-basic/language-reference/statements/raiseevent-statement.md * Add link to RaiseEvent statement in example description Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent ab60dac commit 9a6e16d

File tree

2 files changed

+34
-51
lines changed

2 files changed

+34
-51
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 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+
Declare a `WithEvents` variable in your class to handle events from the timer:
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: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -157,56 +157,46 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042
157157
End Class
158158
' </snippet13>
159159

160-
Class Form1
161-
Inherits Form
162-
Private WithEvents Button1 As New Button
163-
Private WithEvents TextBox1 As New TextBox
164-
165-
' <snippet14>
166-
Private WithEvents mText As TimerState
160+
' <snippet14>
161+
Public Class TimerExample
162+
Private WithEvents mTimer As TimerState
167163
' </snippet14>
168164

169165
' <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)
166+
Public Sub StartCountdownExample()
167+
mTimer = New TimerState()
168+
mTimer.StartCountdown(10.0, 1.0)
176169
End Sub
177170

178-
Private Sub mText_ChangeText() Handles mText.Finished
179-
TextBox1.Text = "Done"
171+
Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime
172+
Console.WriteLine("Time remaining: " & Format(Countdown, "##0.0") & " seconds")
180173
End Sub
181174

182-
Private Sub mText_UpdateTime(ByVal Countdown As Double
183-
) Handles mText.UpdateTime
175+
Private Sub mTimer_Finished() Handles mTimer.Finished
176+
Console.WriteLine("Done")
177+
End Sub
178+
End Class
184179

185-
TextBox1.Text = Format(Countdown, "##0.0")
186-
' Use DoEvents to allow the display to refresh.
187-
My.Application.DoEvents()
180+
Public Class TimerState
181+
Public Event UpdateTime(ByVal Countdown As Double)
182+
Public Event Finished()
183+
Public Sub StartCountdown(ByVal Duration As Double,
184+
ByVal Increment As Double)
185+
Dim SoFar As Double = 0
186+
Do While SoFar < Duration
187+
System.Threading.Thread.Sleep(CInt(Increment * 1000))
188+
SoFar += Increment
189+
RaiseEvent UpdateTime(Duration - SoFar)
190+
Loop
191+
RaiseEvent Finished()
188192
End Sub
193+
End Class
194+
' </snippet15>
189195

190-
Class TimerState
191-
Public Event UpdateTime(ByVal Countdown As Double)
192-
Public Event Finished()
193-
Public Sub StartCountdown(ByVal Duration As Double,
194-
ByVal Increment As Double)
195-
Dim Start As Double = DateAndTime.Timer
196-
Dim ElapsedTime As Double = 0
197-
198-
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
205-
Loop
206-
RaiseEvent Finished()
207-
End Sub
208-
End Class
209-
' </snippet15>
196+
Class Form1
197+
Inherits Form
198+
Private WithEvents Button1 As New Button
199+
Private WithEvents TextBox1 As New TextBox
210200

211201
End Class
212202
End Class

0 commit comments

Comments
 (0)