Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.Net] Time Delay While Keeping UI Functional (Delegates, StopWatch, Threads)
#1
Code:
Public Class Form1

#Region "Form1 Constructor"
    Public Sub New()
        InitializeComponent()
    End Sub
#End Region

    'Start Here
    Private Sub MainMethod()
        Dim Tp = Tuple.Create("message box string", "title", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Me.Pause(2500, New DelegateAfterDelay(AddressOf AfterDelay), Tp)
    End Sub

    'After the Pause, this gets raised
    Delegate Sub DelegateAfterDelay(Tp As Tuple(Of String, String, MessageBoxButtons, MessageBoxIcon))
    Private Sub AfterDelay(Tp As Tuple(Of String, String, MessageBoxButtons, MessageBoxIcon))
        MessageBox.Show(Tp.Item1, Tp.Item2, Tp.Item3, Tp.Item4)
    End Sub

End Class

Public Module DelayModule
    <Runtime.CompilerServices.Extension()> _
    Public Sub Pause(sender As Object, Milliseconds As Integer, DelegateMethod As [Delegate], ParamArray Params As Object())
        Dim T As New Thread(Sub() PauseMethod(sender, Milliseconds, DelegateMethod, Params))
        T.Start()
    End Sub

    Private Sub PauseMethod(sender As Object, Milliseconds As Integer, DelegateMethod As [Delegate], ParamArray Params As Object())
        Dim S As Stopwatch = Stopwatch.StartNew
        While S.ElapsedMilliseconds < Milliseconds
            'Do Nothing
        End While
        DirectCast(sender, Form).Invoke(DelegateMethod, Params)
    End Sub
End Module

Some code I came up with while thinking of ways to create a delay that wouldn't interfere with the UI, and wouldn't use Application.DoEvents(). Here's a cool method Smile

Just test code, use it if you want. Ask questions if you don't understand my code. Nice extension method Smile

Pirate
Reply


Messages In This Thread
[VB.Net] Time Delay While Keeping UI Functional (Delegates, StopWatch, Threads) - by AceInfinity - 09-03-2012, 11:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Support Forums .NET Threads Compilation Fragma 64 20,200 11-06-2019, 11:35 PM
Last Post: alexathomson
  Delay with loop macas5 1 559 05-19-2010, 10:43 AM
Last Post: thanasis2028

Forum Jump:


Users browsing this thread: 1 Guest(s)