用方法的异步调用,在函数计算的过程中窗体不会变白,窗体上的的进度条也可以继续
Private conponents As System.ComponentModel.Container = Nothing
Public Delegate Function MyDelegate() As Integer
Dim iscanceled As Boolean = False
Dim re As Integer
Dim asyncDelegate As New MyDelegate(AddressOf Me.ProcessTask)
Dim ar As IAsyncResult
Public Function ProcessTask() As Integer
Dim mythread As System.Threading.Thread
Dim i As Integer
For i = 0 To 100
If (Me.iscanceled) Then Exit Function
ProgressBar1.Value = i
mythread.Sleep(100)
Next
Return i
End Function
Public Sub resultsCB(ByVal result As IAsyncResult)
re = asyncDelegate.EndInvoke(ar)
Dim mi As New MethodInvoker(AddressOf Me.updateUI)
Me.BeginInvoke(mi)
End Sub
调用你的计算方法时请写如下代码:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.iscanceled = False
Me.Cursor = Cursors.WaitCursor
Dim cb As New AsyncCallback(AddressOf Me.resultsCB)
ar = asyncDelegate.BeginInvoke(cb, Nothing)
Me.Cursor = Cursors.Default
End Sub