Calling user interface controls across threads is not allowed.
The following shows working code:
private void Form1_Load(object sender, EventArgs e)
{
Thread t = new Thread(ThreadProc);
t.Start();
}
private void ThreadProc()
{
while (true)
{
Thread.Sleep(1000);
string[] tmpstr = new string[]{
"a",
"b",
"c"
};
dataGridView1.Invoke(new MethodInvoker(delegate
{
// Put code to run on the UI thread here.
dataGridView1.Rows.Add(tmpstr);
}));
}
}
Be aware that calling back to the UI thread in this manner can be a source of performance problems. But a rate of once in 10 seconds is nowhere near the rate that causes problems. Many times per second is problematic. I'm just bringing this up because it is acommon placewhere people run into problems with this technique. In such cases, it is best to consolidate updates or evenuse a timer thatperiodically pulls any new data from a buffer populated by the background thread.