Os dejo un corto sobre Async, Await, como ya sabréis mejora mucho la respuesta del GUI… asi que ya no teneis excusa para dejar a la peña con el relojto esperando que terminen ciertas tareas.. :-))
Private cts As CancellationTokenSource Async Sub btnStart_Clik(sender As Object, e As EventArgs) Handles btnStart.Click btnStart.Enabled = False Try cts = New CancellationTokenSource Await Task.Run(Sub() longThingsProcessing(cts.Token) End Sub, cts.Token) Catch ex As Exception MessageBox.Show(ex.Message) End Try btnStart.Enabled = True End Sub Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click cts.Cancel() End Sub Sub longThingsProcessing(ct As CancellationToken) Try Dim charNumber As Integer = 0 For x As Integer = 0 To 200000 ct.ThrowIfCancellationRequested() charNumber += 1 If charNumber > 255 Then charNumber = 0 updateTitle(String.Format("Doing loop : {0}, Char {1}", x.ToString, Chr(charNumber))) Next Catch ex As Exception updateTitle("Operation was cancelled") End Try End Sub Delegate Sub textUpdate(text As String) Sub updateTitle(_text As String) If Me.InvokeRequired Then Dim _me As New textUpdate(AddressOf updateTitle) Me.Invoke(_me, _text) Else Me.Text = _text Me.Refresh() End If End Sub