title: Cross-thread operation not valid: Control 'RichTextBox' accessed from a thread other than the thread it was created on
body: <p>I'm using telegram APis to send a message to the client.
  The code I'm using is:</p>
  <pre class="lang-vb prettyprint-override"><code>Imports System.Threading
  Imports Telegram.Bot
  Imports Telegram.Bot.Extensions.Polling
  Imports Telegram.Bot.Types
  Imports Telegram.Bot.Types.Enums
      
  Public Class form1
      Private Async Function HandleUpdateAsync(botClient As ITelegramBotClient,
                                               update As Update,
                                               cancellationToken As CancellationToken) As Task
      
          If update.Message IsNot Nothing Then
              If Not String.IsNullOrEmpty(update.Message.Text) Then
                  Select Case update.Message.Text
                      Case &quot;/0&quot;
                           Await botClient.SendTextMessageAsync(&quot;chatid&quot;, RichTextBox1.Text)
                      Case &quot;/star2t&quot;
                          Await botClient.SendTextMessageAsync(&quot;chatid&quot;, &quot;text&quot;)
                  End Select
              End If
          End If
      End Function
      
      Private Function HandleErrorAsync(botClient As ITelegramBotClient,
                                        exception As Exception,
                                        cancellationToken As CancellationToken) As Task
          Console.WriteLine(exception)
          Return Task.CompletedTask
      End Function
      
      Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          'invio messaggio 
          Dim bot = New TelegramBotClient(&quot;token id&quot;)
          Await bot.SendTextMessageAsync(&quot;chatid&quot;, &quot;Test&quot;)
      End Sub
      
      Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          Dim bot = New TelegramBotClient(&quot;My id&quot;)
    
          Dim cts = New CancellationTokenSource()
          Dim options = New ReceiverOptions With {
              .AllowedUpdates = Array.Empty(Of UpdateType)()
          }
          bot.StartReceiving(updateHandler:=AddressOf HandleUpdateAsync, errorHandler:=AddressOf HandleErrorAsync, options, cts.Token)
          Console.ReadLine()
      End Sub
  End Class
  </code></pre>
  <p>It's sending the message with</p>
  <pre class="lang-vb prettyprint-override"><code>Await botClient.SendTextMessageAsync(&quot;chatid&quot;, &quot;text&quot;)
  </code></pre>
  <p>but its not sending it</p>
  <p>with</p>
  <pre class="lang-vb prettyprint-override"><code>Await botClient.SendTextMessageAsync(&quot;chatid&quot;, Richtextbox5.text)
  </code></pre>
  <p>I'm not getting any error at runtime or at debug, but checking the Immediate Window on VS while debugging I see that there is a cross threading issue:</p>
  <blockquote>
  <p>exception thrown: 'System.InvalidOperationException' in System.Windows.Forms.dll
  <br>System.InvalidOperationException: Cross-thread operation not valid: Control 'RichtextBox5' accessed from a thread other than the thread it was created on
  <br>in System.Windows.Forms.Control.get_Handle()
  <br>in System.Windows.Forms.RichTextBox.StreamOut(Stream data, Int32 flags, Boolean includeCrLfs)
  <br>in System.Windows.Forms.RichTextBox.StreamOut(Int32 flags)
  <br>in System.Windows.Forms.RichTextBox.get_Text()
  <br>in HotBit_APIs.Form1.VB$StateMachine_17_HandleUpdateAsync.MoveNext()
  <br>in C:\Users\Mattia\source\repos\HotBit APIs\HotBit APIs\Form1.vb:line 81</p>
  </blockquote>
  <p>I've been looking online for a solution and I came accross this code</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub UpdateTextBox(text As String)
      If TextBox1.InvokeRequired Then
          'We are on a secondary thread.
          TextBox1.Invoke(New Action(Of String)(AddressOf UpdateTextBox), text)
      ecc
  </code></pre>
  <p>or  <code>yourForm.Invoke(new Action(() =&gt; {GUI code here}))</code></p>
  <p>but I don't know exactly how to implement with my code.
  Any idea on how I can make it?</p>