In Visual Basic, asynchronous calls and event handling are often implemented in different ways, depending on the version of the language and the type of application. In classic WinForms applications using VB.NET, event queue handling usually involves calling Application.DoEvents(). This method allows the event handler to "release control" so that other messages can be processed without blocking the main thread:
While loading Application.DoEvents() 'Allows UI to respond to user actions End While
For asynchronous task execution in .NET with VB.NET, the following are used:
BackgroundWorker — a WinForms component for offloading long operations to a separate thread with safe UI thread interaction.Async/Await — a convenient modern asynchronous system introduced in .NET 4.5, allowing you to write asynchronous code "like synchronous":Public Async Function LoadDataAsync() As Task Dim result As String = Await GetWebDataAsync() TextBox1.Text = result End Function
Differences:
DoEvents simply processes the UI message queue and does not create new threads.BackgroundWorker shifts work to a separate thread with an event for safely returning to the UI thread.Async/Await implements asynchronicity while encapsulating control return to the UI thread.What is the danger of using
DoEventsin a loop when it is required to load a file from disk and update the ProgressBar?
Answer:
DoEvents temporarily passes control to other Windows events but does not release the thread. If used in heavy loops (for example, reading a large file while updating the UI), unexpected bugs may occur: user events and mouse events will be processed, which can lead to the re-initiation of processing or even "hanging" the application. If heavy loading is expected, BackgroundWorker or Task should be used.
Example of wrong approach:
For i = 1 To 1000000 ProgressBar1.Value = i / 10000 Application.DoEvents() 'Does not free the main thread! Next
History
DoEvents was used to update the progress indicator within a long file reading loop. As a result, the interface "froze": if the user clicked the "Open" button again, a second file reading process started, intermingling with the first, causing data distortion and application crashes.History
DoEvents in a loop, as he was not familiar with BackgroundWorker. When updating a huge set of points, the application began to lag and subsequently "crashed" due to message queue overflow in Windows.History
Asynchronous calls were used via Async/Await, not understanding that long-running tasks cannot be started in the "hot" UI thread without Await Task.Run(...). The result: the interface did not respond to clicks, and the progress bar was not updated, as the long method still executed on the main thread.