Keep Your Background Processes Running When Displaying Message Box

Author: Microsoft

In Visual Basic, if you make a call to the MsgBox function, all other background processes that you may have running (counters, timer events, etc) are stopped until the user acknowledges the Msgbox dialog box. This can be potentially devastating if you write an application that runs unattended.

To overcome this problem, you must use the Windows API call for the MessageBox function. It looks and acts the same as the Visual Basic "msgbox" function, but does not stop the background processes from running.

The Code below show the difference between the two methods.
Press the first button to display message box with VB MsgBox function, and the second button to display it via API.


Add 2 Command Button (named Command1 and Command2)
Add 1 Label (named Label1)
Add 1 Timer Control. Set the Timer Interval property to 1

Module Code

Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As _
Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As _
Long) As Long

Form Code

Private Sub Command1_Click()
MsgBox "The Timer STOPS!"
End Sub

Private Sub Command2_Click()
MessageBox Me.hwnd, "Notice the timer does not stop!", "API Call", _
vbOKOnly + vbExclamation
End Sub

Private Sub Timer1_Timer()
Label1.Caption = Time
End Sub

Go Back