hi,
I'm trying to test threading using datagridview.
The scenario is:
1- A form has (button1), (datagridview1).
2- Once I click (button1) a thread will start and start adding rows until I click again the (button1).
I did it like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
if (thread.ThreadState == ThreadState.Running)
{
thread.Abort();
return;
}
thread.Start();
}
public void WorkThreadFunction()
{
try
{
dataGridView1.Rows.Add(new object[] { "test" });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But unfortunately, it didn't work and I got an error:
Cross-thread operation not valid
Please help me with an easy tips (edit my code if you can) because I'm not that much experienced with such things.
Thanks,