I have a function in VB.Net that splits by lines an string, then it iterates one by one to print it on serialPort (not yet implemented) and also spits it on a RichTextBox, but the code is blocking the UI, until it ends, but it's necessary to have access to the UI, if you want to Stop. Here is my code...
Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim t As New Threading.Thread(Sub()
refreshRichTextBox(RichTextBox1, vbCrLf + "======================================================")
Dim GCODES As String() = GCode.Split(New [Char]() {CChar(vbCrLf)})
Dim TaskList = GCODES.Select(Function(x) Send(x))
Task.WhenAll(TaskList.ToArray())
End Sub)
t.Start()
End Sub
Private Async Function Send(line As String) As Task(Of String)
Await refreshRichTextBox(RichTextBox1, line)
Return "A"
End Function
Private Async Function refreshRichTextBox(ByVal rtextBox As RichTextBox, str As String) As Task
Try
Await Task.Run(Function()
Me.RichTextBox1.Invoke(Function()
Me.RichTextBox1.AppendText(str + vbCrLf)
Me.RichTextBox1.SelectionStart = rtextBox.Text.Length
Me.RichTextBox1.ScrollToCaret()
End Function)
End Function)
Catch ex As Exception
End Try
End Function
Thanks.
I have already tried to create multiple threads, use async functions and pray.