Parameters:

Public Sub New(ByVal TaskName As String, ByVal ExecutionDelayTime As Integer, ByVal BlockCurrentThread As Boolean, Optional ByVal Message As String = “”)

ExecutionDelayTime – Timer interval in milliseconds. (How long to wait before executing a task)
TaskName – The name of the Task which will be chosen from Select Case statement
BlockCurrentThread – If set to True the executed task will run in the same thread as the method which called the ExecuteTask sub. If set to False the executed task will run in a new thread.
Message – (Optional) a custom text to display in MsgBox.

Usage:

Following example will start two tasks. The task “MsgBox” after 5 seconds and “Application.Exit” after 10 seconds:

Dim _task As New Tasks("MsgBox", 5000, False, "The application will be turned off.")
 _task.ExecuteTask()

_task = New Tasks("Application.Exit", 10000, False)
 _task.ExecuteTask()

Code:

            

Public Class Tasks

Private WithEvents _timer As System.Timers.Timer

    Private _TaskName As String
    Private _Message As String
    Private _ExecutionDelayTime As Integer
    Private _BlockCurrentThread As Boolean

    Public Sub New(ByVal TaskName As String, ByVal ExecutionDelayTime As Integer, ByVal BlockCurrentThread As Boolean, Optional ByVal Message As String = "")
        _TaskName = TaskName
        _ExecutionDelayTime = ExecutionDelayTime
        _BlockCurrentThread = BlockCurrentThread
        _Message = Message
    End Sub

    Private Sub TimerStart()
        _timer = New System.Timers.Timer(_ExecutionDelayTime)
        _timer.Start()
    End Sub

    Public Sub ExecuteTask()

        If _BlockCurrentThread = True Then
            TimerStart()
        Else
            Dim NewThread As System.Threading.Thread
            NewThread = New System.Threading.Thread(AddressOf TimerStart)
            NewThread.Start()
        End If
    End Sub

    Private Sub StartConfiguredTask() Handles _timer.Elapsed

        _timer.Stop() ' Stops the Timer to avoid continous execution upon Elapsed event.

        Loger.WriteToLogFile("StartConfiguredTask()", "[ BlockCurrentThread: " & _BlockCurrentThread & " | Thread ID: " & System.Threading.Thread.CurrentThread.ManagedThreadId & " ] - TaskName: " & _TaskName, Cache.LogFilePath)

        Select Case _TaskName

            Case "Application.Exit"
                Application.Exit()
            Case "MsgBox"
                MsgBox(_Message)
            Case Else
                Loger.WriteToLogFile("StartConfiguredTask", "Error: NOT Supported Task Name", Cache.LogFilePath)
        End Select

    End Sub
End Class