VBMAN.Delay - Delayed Execution Object
Overview
VBMAN.Delay provides delayed execution and asynchronous callback functionality, allowing code execution after a specified time without blocking the main thread, suitable for scenarios requiring delayed operations.
Core Features
- Non-blocking Delay: Does not freeze the UI
- Callback Support: Supports object method callbacks
- Cancellable: Can cancel pending delayed tasks
- Chain Calling: Fluent API design
Methods
Callback
Set callback object and method
vb
Public Function Callback(ByVal obj As Object, ByVal methodName As String) As cDelayParameters:
obj- Callback objectmethodName- Method name to call
Example:
vb
' Set callback
VBMAN.Delay.Callback Me, "DelayedMethod"CountDown
Start countdown (milliseconds)
vb
Public Function CountDown(ByVal milliseconds As Long) As cDelayExample:
vb
' Execute after 2 seconds
VBMAN.Delay.Callback(Me, "ShowMessage").CountDown 2000
' Execute after 5 seconds
VBMAN.Delay.Callback(Me, "AutoSave").CountDown 5000Comprehensive Examples
Example 1: Delayed Message Display
vb
' In form module
Public Sub ShowDelayedMessage()
VBMAN.Delay.Callback Me, "ShowWelcome"
VBMAN.Delay.CountDown 1000
End Sub
Public Sub ShowWelcome()
MsgBox "Welcome to the system!"
End Sub
' Usage
Private Sub Form_Load()
ShowDelayedMessage
End SubExample 2: Asynchronous Main Form Display (from cs-auther case)
vb
' Reason for asynchronous main form display:
' Because bUser.Info executes inside the TCP receive handler, and fLogin also listens to TCP events.
' If fLogin is unloaded directly in Info, it causes event re-entry conflicts.
' Therefore, use VBMAN.Delay.Callback(Me, "ShowMainForm").CountDown 2000 to execute asynchronously after 2 seconds delay.
Public Sub ShowMainForm()
fLogin.Hide
Form1.Show
End Sub
' Call after successful login
Private Sub LoginSuccess()
VBMAN.Delay.Callback(Me, "ShowMainForm").CountDown 2000
End SubExample 3: Auto-save Feature
vb
Private Sub TextEditor_Change()
' Auto-save 3 seconds after content change
VBMAN.Delay.Callback(Me, "AutoSave").CountDown 3000
End Sub
Public Sub AutoSave()
' Save file
SaveFile TextEditor.Text, CurrentFilePath
VBMAN.Toast.Show "Auto-saved", 1000
End SubExample 4: Retry Mechanism
vb
Private retryCount As Integer
Private Sub ConnectToServer()
If Not TryConnect() Then
retryCount = retryCount + 1
If retryCount <= 3 Then
VBMAN.Toast.Show "Connection failed, retrying in " & retryCount & " seconds...", 2000
VBMAN.Delay.Callback(Me, "ConnectToServer").CountDown retryCount * 1000
Else
MsgBox "Connection failed, please check network settings"
End If
End If
End SubExample 5: Delayed Shutdown
vb
Private Sub ShutdownWithDelay()
VBMAN.Toast.Show "System will shut down in 5 seconds", 5000
VBMAN.Delay.Callback(Me, "DoShutdown").CountDown 5000
End Sub
Public Sub DoShutdown()
' Perform cleanup
Cleanup
' Close application
Unload Me
End SubBest Practices
- Avoid Circular Calls: Be careful to prevent infinite recursive delayed calls
- Object Lifetime: Ensure callback object still exists when delay executes
- Thread Safety: Pay attention to thread safety when using in multi-threaded environments
- Error Handling: Add error handling in callback methods
- Cancel in Time: Cancel promptly if delayed execution is no longer needed