A friend of mine who writes in VB at his company was telling me about a situtation they're having with long running tasks on their production application. Basically they are polling a database for changes (if it was sql 2005 instead of mysql i'd suggest notification services... not sure if mysql has equivalent). Anyhow, this long running task is locking up the UI so i suggested he use delegates and their built in async pattern to handle this since it's easy constructed and utilized in forms, then he asked how he'd update the UI when the polling completes. In windows forms, there's actually a BeginInvoke method that will let you launch a delegate. I put a tiny and simple example together to get my point across. Here's the form code (basically theres a button and a label on the form
Imports System.Threading
Public Class Form1
Private Delegate Sub RunAsyncDelegate(ByRef iar As IAsyncResult)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d As RunAsyncDelegate = New RunAsyncDelegate(AddressOf RunAsync)
d.BeginInvoke(Nothing, AddressOf NotifyFinished, Nothing)
End Sub
Private Sub RunAsync(ByRef iar As System.IAsyncResult)
Thread.Sleep(10000)
End Sub
Private Delegate Sub NotifyFinishedDelegate(ByVal iar As System.IAsyncResult)
Private Sub NotifyFinished(ByVal iar As System.IAsyncResult)
If Me.InvokeRequired Then
Dim d As NotifyFinishedDelegate = New NotifyFinishedDelegate(AddressOf NotifyFinished)
Me.BeginInvoke(d, iar)
Else
Me.Label1.Text = "Work is Finished"
End If
End Sub
End Class