diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index e385743bd2e2e..a84f9af896120 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -54,26 +54,19 @@ RaiseEvent eventname[( argumentlist )] 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. - 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". + 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". - The code for `Form1` specifies the initial and terminal states of the form. It also contains the code executed when events are raised. - - 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. - - Add a `WithEvents` variable to the declarations section of the `Form1` class. + Declare a `WithEvents` variable in your class to handle events from the timer: [!code-vb[VbVbalrEvents#14](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#14)] ## Example 2 - Add the following code to the code for `Form1`. Replace any duplicate procedures that may exist, such as `Form_Load`, or `Button_Click`. + 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. [!code-vb[VbVbalrEvents#15](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb#15)] - 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". - -> [!NOTE] -> 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). + 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". ## See also diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb index 1100b9e5787df..3b30377dfd090 100644 --- a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrEvents/VB/Class1.vb @@ -157,56 +157,46 @@ Class Class306ff8ed74dd4b6abd2fe91b17474042 End Class ' - Class Form1 - Inherits Form - Private WithEvents Button1 As New Button - Private WithEvents TextBox1 As New TextBox - - ' - Private WithEvents mText As TimerState + ' + Public Class TimerExample + Private WithEvents mTimer As TimerState ' ' - Private Sub Form1_Load() Handles MyBase.Load - Button1.Text = "Start" - mText = New TimerState - End Sub - Private Sub Button1_Click() Handles Button1.Click - mText.StartCountdown(10.0, 0.1) + Public Sub StartCountdownExample() + mTimer = New TimerState() + mTimer.StartCountdown(10.0, 1.0) End Sub - Private Sub mText_ChangeText() Handles mText.Finished - TextBox1.Text = "Done" + Private Sub mTimer_UpdateTime(ByVal Countdown As Double) Handles mTimer.UpdateTime + Console.WriteLine("Time remaining: " & Format(Countdown, "##0.0") & " seconds") End Sub - Private Sub mText_UpdateTime(ByVal Countdown As Double - ) Handles mText.UpdateTime + Private Sub mTimer_Finished() Handles mTimer.Finished + Console.WriteLine("Done") + End Sub + End Class - TextBox1.Text = Format(Countdown, "##0.0") - ' Use DoEvents to allow the display to refresh. - My.Application.DoEvents() + Public Class TimerState + Public Event UpdateTime(ByVal Countdown As Double) + Public Event Finished() + Public Sub StartCountdown(ByVal Duration As Double, + ByVal Increment As Double) + Dim SoFar As Double = 0 + Do While SoFar < Duration + System.Threading.Thread.Sleep(CInt(Increment * 1000)) + SoFar += Increment + RaiseEvent UpdateTime(Duration - SoFar) + Loop + RaiseEvent Finished() End Sub + End Class + ' - Class TimerState - Public Event UpdateTime(ByVal Countdown As Double) - Public Event Finished() - Public Sub StartCountdown(ByVal Duration As Double, - ByVal Increment As Double) - Dim Start As Double = DateAndTime.Timer - Dim ElapsedTime As Double = 0 - - Dim SoFar As Double = 0 - Do While ElapsedTime < Duration - If ElapsedTime > SoFar + Increment Then - SoFar += Increment - RaiseEvent UpdateTime(Duration - SoFar) - End If - ElapsedTime = DateAndTime.Timer - Start - Loop - RaiseEvent Finished() - End Sub - End Class - ' + Class Form1 + Inherits Form + Private WithEvents Button1 As New Button + Private WithEvents TextBox1 As New TextBox End Class End Class