title: Safe Multi-thread access in C#
body: <p>I am trying to implement a chat. I have taken a code <a href="https://www.youtube.com/watch?v=BDVfpPq3weo" rel="nofollow noreferrer">here</a></p>
  
  <p>When I send messages I have an error: </p>
  
  <blockquote>
    <p>Control 'ListMessage' name accessed from a thread other than the
    thread it was created on.</p>
  </blockquote>
  
  <p>where ListMessage is a ListView added with Visual Studio.</p>
  
  <pre><code>       private void buttonSend_Click(object sender, EventArgs e)
          {
              //convert string message to byte[]
              ASCIIEncoding aEncoding = new ASCIIEncoding();
              byte[] sendingMessage = new byte[1500];
              sendingMessage = aEncoding.GetBytes(textMessage.Text);
              //sending the encoded message
              sck.Send(sendingMessage);
              //adding to the list box
              listMessage.Items.Add("Me:" + textMessage.Text);
              textMessage.Text = "";
          }
  
          private void MessageCallBack(IAsyncResult aResult)
          {
              try
              {
                  byte[] receivedData = new byte[1500];
                  receivedData = (byte[])aResult.AsyncState;
                  //converting byte[] to string
                  ASCIIEncoding aEncoding = new ASCIIEncoding();
                  string receivedMessage = aEncoding.GetString(receivedData);
  
                  //Adding this message into list box
  ///// error is given in the following line/////
                  listMessage.Items.Add("Friend:" + receivedMessage);
  
                  buffer = new byte[1500];
                  sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
          }
  </code></pre>
  
  <p>I have tried to use Invoke as described <a href="https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls" rel="nofollow noreferrer">here</a> but I have errors </p>
  
  <pre><code>        private void MessageCallBack(IAsyncResult aResult)
          {
              try
              {
                  byte[] receivedData = new byte[1500];
                  receivedData = (byte[])aResult.AsyncState;
                  //converting byte[] to string
                  ASCIIEncoding aEncoding = new ASCIIEncoding();
                  string receivedMessage = aEncoding.GetString(receivedData);
  
                  //Adding this message into list box
                  //listMessage.Items.Add("Friend:" + receivedMessage);
                  listMessage.AddItemSafe("Friend:" + receivedMessage);
  
                  buffer = new byte[1500];
                  sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString());
              }
          }
  
          private void AddItemSafe(string text)
          {
              if (this.InvokeRequired)
              {
                  AddItemSafeCallback a = new AddItemSafeCallback(AddItemSafe);
                  this.Invoke(a, new object[] { text });
              }
              else
              {
                  this.Items.Add(text);
              }
          }
  
          delegate void AddItemSafeCallback(string text);
  </code></pre>
  
  <p>Erros are:</p>
  
  <ul>
  <li>'ListBox' does not contain a definition fo 'AddItemSafe' and no extension methode. 'AddItemSafe' accepting a first argument of type 'ListBox'could be found (are you missing a using directive or an assembly reference'</li>
  <li>'MyUserControl' does not contain a definition fo 'AddItemSafe' and no extension methode. 'Item' accepting a first argument of type
  'ListBox'could be found (are you missing a using directive or an
  assembly reference'</li>
  </ul>
  
  <p>My questions:</p>
  
  <ul>
  <li>Do you believe the way I try to fix to initial problem is the good
  one ? </li>
  <li>If yes, what is the problem with the solution I try to
  implement</li>
  </ul>