Skip to content

Instantly share code, notes, and snippets.

@MakotoUwaya
Last active December 2, 2020 12:44
Show Gist options
  • Select an option

  • Save MakotoUwaya/89f1672ea50d6a6fc1ea9919a89025a6 to your computer and use it in GitHub Desktop.

Select an option

Save MakotoUwaya/89f1672ea50d6a6fc1ea9919a89025a6 to your computer and use it in GitHub Desktop.
Tasks UI Invoke Sample
Public Class Form1
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.F8
Dim fc As New FormController()
fc.AsyncAction(Me)
End Select
End Sub
End Class
Imports System.Threading.Tasks
Public Class FormController
Delegate Sub AddRowDelegate(form As Form1, value As Integer)
Public Sub AsyncAction(form As Form1)
form.DataGridView1.Rows.Clear()
Dim t As New Task(Sub()
For i As Integer = 0 To 19999 Step 1
form.DataGridView1.Invoke(New AddRowDelegate(AddressOf AddRow), form, i)
Next
End Sub, TaskCreationOptions.PreferFairness)
t.Start()
t.ContinueWith(Sub() MessageBox.Show("Finish!"))
End Sub
Private Sub AddRow(form As Form1, value As Integer)
form.DataGridView1.Invoke(
Sub()
form.DataGridView1.Rows.Add(value + 1, value + 2, value + 3, value + 4, value + 5)
form.DataGridView1.Update()
End Sub)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment