Q:

Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on

I have declared one formClosing event procedure on form closing 
here is my code.I'm designing a notepad using c# and before closing the form or my notepad app it should ask user whether he/she wants to save it or not ..
private void Form13_FormClosing(object sender, FormClosingEventArgs e)
    {

        if (this.Text == "Untitled-FileEditor")
        {
            if (richTextBox1.Text.Length > 0)
            {
                DialogResult dr = MessageBox.Show("Do u want to save changes to untitled", "FileEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    saveFileDialog1.FileName = "NewText";
                    saveFileDialog1.Filter = "Text Files(*.txt)|*.txt";
                    DialogResult d = saveFileDialog1.ShowDialog();
                    if (d == DialogResult.OK)
                    {
                        string TextFilePath = saveFileDialog1.FileName;
                        richTextBox1.SaveFile(TextFilePath, RichTextBoxStreamType.PlainText);
                        richTextBox1.Text = "";
                    }
                }
                if (dr == DialogResult.No)
                {
                    //richTextBox1.Text = "";
                    this.Close();
                }
            }
            else if (richTextBox1.Text.Length == 0)
            { this.Close(); }
        }
        else if (this.Text != "Untitled-FileEditor")
        {
            if (richTextBox1.Modified)
            {
                DialogResult dr = MessageBox.Show("Do u want to save changes to " + openFileDialog1.FileName, "FileEditor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    saveFileDialog1.FileName = "NewText";
                    saveFileDialog1.Filter = "Text Files(*.txt)|*.txt";
                    DialogResult d = saveFileDialog1.ShowDialog();
                    if (d == DialogResult.OK)
                    {
                        string TextFilePath = saveFileDialog1.FileName;
                        richTextBox1.SaveFile(TextFilePath, RichTextBoxStreamType.PlainText);
                        richTextBox1.Text = "";
                    }
                }
                if (dr == DialogResult.No)
                {
                    //richTextBox1.Text = "";
                    this.Close();
                }
            }
            if (!richTextBox1.Modified)
            {
                this.Text = "Untitled-FileEditor";
                //richTextBox1.Text = "";
                this.Close();
            }
        }
    }

Error showing - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.

A:

This means that you are trying to access/modify data from an other thread than it is allowd to.
Please try:
richtextBox1.Invoke(new Action(richtextBox1.Text = "sample text")); //just a sample

Instead of richtextBox1.Text = "sample text" you write the code of the line where the error appears.
Maybe you should perform your actions within OnClosed event:
protected override void OnClosed(EventArgs e)
{
    //your code here
    base.OnClosed(e);
}

