Skip to content

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 cDelay

Parameters:

  • obj - Callback object
  • methodName - 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 cDelay

Example:

vb
' Execute after 2 seconds
VBMAN.Delay.Callback(Me, "ShowMessage").CountDown 2000

' Execute after 5 seconds
VBMAN.Delay.Callback(Me, "AutoSave").CountDown 5000

Comprehensive 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 Sub

Example 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 Sub

Example 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 Sub

Example 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 Sub

Example 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 Sub

Best Practices

  1. Avoid Circular Calls: Be careful to prevent infinite recursive delayed calls
  2. Object Lifetime: Ensure callback object still exists when delay executes
  3. Thread Safety: Pay attention to thread safety when using in multi-threaded environments
  4. Error Handling: Add error handling in callback methods
  5. Cancel in Time: Cancel promptly if delayed execution is no longer needed

VB6 and LOGO copyright of Microsoft Corporation