title: Cross threading problem: Invalid operationexception was handeled
body: <p>I am making a program that import emails from txt files and send a message to them , but i am facing a problem , currently i am using a thread for the sending mail method to prevent the program to stop responding , the exact problem title is:</p>
  
  <pre>Invalid operationexception was handeled
  >>> Cross-thread operation not valid:
      Control 'richTextBox1' accessed from a thread other than the thread it was created on.</pre>
  
  <p>here is the code  </p>
  
  <pre><code>    int success = 0;
      int failed = 0;
      int total = 0;
      bool IsRunning;
  
  List&lt;string&gt; list = new List&lt;string&gt;();
  
  private void addmails()
      {
          string path = textBox2.Text;
  
          foreach (string line in File.ReadAllLines(path))
          {
              list.Add(line);
          }
          IsRunning = true;
      }
      private void sendmails(object sender, DoWorkEventArgs e)
      {
          if (IsRunning == true)
          {
              if (checkBox1.Checked != true)
              {
                  SmtpClient client = new SmtpClient(comboBox1.Text);
                  client.Credentials = new NetworkCredential(textBox6.Text, textBox7.Text);
                  MailMessage message = new MailMessage();
                  message.From = new MailAddress(textBox3.Text, textBox1.Text);
                  message.Subject = textBox4.Text;
                  //message.Body = richTextBox1.Text;
                  if (textBox5.Text != "")
                  {
                      message.Attachments.Add(new Attachment(textBox5.Text));
                  }
  
                  foreach (string eachmail in list)
                  {
                      if (IsRunning == true)
                      {
                          try
                          {
                              message.To.Add(eachmail);
                              client.Send(message);
                              listBox1.Items.Add("Successfully sent the message to  : " + eachmail);
                              success++;
                          }
                          catch
                          {
                              listBox1.Items.Add("Failed to send the message to  : " + eachmail);
                              failed++;
                          }
                          message.To.Clear();
  
                          total++;
  
                          Thread.Sleep(15);
  
                          label18.Text = total.ToString();
                          label19.Text = success.ToString();
                          label21.Text = failed.ToString();
  
                      }
                      else
                      {
                          break;
                      }
                  }
  
                  IsRunning = false;
                  button3.Text = "Send";
              }
  
          }
      }
  
      private void button3_Click(object sender, EventArgs e)
      {
          if (button3.Text == "Send")
          {
              tabControl1.SelectedTab = tabPage3;
              button3.Text = "Stop";
  
              addmails();
  
             // IsRunning = true;
  
              Thread t2 = new Thread(sendmails); // when using that thread i get a cross threading error
              t2.Start();
  
          }
          else
          {
              IsRunning = false;
              button3.Text = "Send";
              MessageBox.Show("Sending Mails Operation has been terminated","Abort",MessageBoxButtons.OK,MessageBoxIcon.Information);
          }
  </code></pre>