Can someone tell me why the below code is not working?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t = new Thread(rtb2);
t.Start();
}
private void rtb2()
{
try
{
richTextBox1.Text = "1";
}
catch(InvalidOperationException ex)
{
MessageBox.Show("HI");
}
}
}
The problem is your attempting to modify a Winforms UI element from a background thread. This is specifically not allowed by the WinForms model. UI elements can only be modified from the main thread. You need to use
Control.Invokein order to get the context back onto the appropriate thread.