Don’t stop timers when a MsgBox is active

Don’t stop timers when a MsgBox is active

Not all developers know that Timer controls in compiled VB5 and VB6 applications aren’t stopped by MsgBox statement, so the Timer’s Timer event procedure is regularly executed even when the message box is being displayed on the screen.

However, inside interpreted applications under all VB versions, Timers are stopped when a MsgBox is active, which means that, if you rely on the code in the Timer event procedure, you can’t completely test and debug your app withing the VB environment.

To do so, you must use the MessageBox API function instead of the built-in MsgBox function. The API function has a similar syntax, but it takes an additional argument that states the parent window of the message box dialog (you can use Me.hWnd), and the order of the Title and Buttons arguments is reversed. Even more interesting, all the VB constants can be used without any problem for the Buttons argument:

Private 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

To test this function, create a new form and place one timer, one label, and two command buttons on its surface, then type this code:

Private Sub Command1_Click()    MsgBox "The Timer STOPS!", vbOKOnly, "Regular MsgBox"End SubPrivate Sub Command2_Click()    MessageBox Me.hWnd, "The Timer DOESN'T STOP", "MessageBox API function", _        vbOKOnly + vbExclamationEnd SubPrivate Sub Timer1_Timer()Label1.Caption = TimeEnd Sub

Even better, you can wrap the API function into a MsgBox-like function that takes the same arguments in the same order:

Function MsgBox2(Prompt As String, Optional Buttons As VbMsgBoxStyle, _    Optional Title As Variant) As VbMsgBoxResult    If IsMissing(Title) Then Title = App.Title    MsgBox2 = MessageBox(Screen.ActiveForm.hWnd, Prompt, Title, Buttons)End Function

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as