0

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.

Fabian Q
  • 1
  • 1
  • 1
    `BackgroundWorker` is a bit old-fashioned, but good for unblocking the UI when there is a single long-running process – SSS May 09 '23 at 04:35
  • How is it blocking the UI? If you try to move the window around, does it move? – Paulo Morgado May 09 '23 at 07:25
  • @PauloMorgado yes, if I try to move the window it doesn't move. – Fabian Q May 09 '23 at 20:11
  • 2
    Maybe I could be wrong about this, but you've got a mishmash of threading and async going on, throw in you're invoking the UI thread as well, all seems a little over complicated to me. My advice to is to either use Threading OR async methods and not mix them. That said, being a bit old school, though I would fall back to a BackgroundWorker – Hursey May 09 '23 at 21:48
  • 1
    Your code seems to only have UI operations. What really asynchronous operations are you trying to execute. – Paulo Morgado May 10 '23 at 07:45
  • The following may be helpful: https://stackoverflow.com/a/69946343/10024425 (see "Form1.vb") and https://stackoverflow.com/a/74895268/10024425 (see "Form1.vb). – Tu deschizi eu inchid May 12 '23 at 18:46

0 Answers0