I started my thread once again (old: DataGridView Multithread update crash) and tested implement invoke from this thread: (How do I update the GUI from another thread?), but still face weird issue:
Start multithread update with 0 thread sleep, and I face this issue:
private void threadingFunction()
{
int ThreadsNumber = dataGridView1.Rows.Count;
List<Task> tasks = new List<Task>();
for (int i = 0; i < ThreadsNumber - 1; i++)
{
tasks.Add(Task.Factory.StartNew(() => Update(i)));
Thread.Sleep(0);
}
Task.WaitAll(tasks.ToArray());
}
void Update(int i)
{
BeginInvoke((Action)delegate ()
{
dataGridView1[4, i].Value = "heheh";
});
}
result image: result image thread sleep 0
and with thread sleep 1 ms it work fine
private void threadingFunction()
{
int ThreadsNumber = dataGridView1.Rows.Count;
List<Task> tasks = new List<Task>();
for (int i = 0; i < ThreadsNumber - 1; i++)
{
tasks.Add(Task.Factory.StartNew(() => Update(i)));
Thread.Sleep(1);
}
Task.WaitAll(tasks.ToArray());
}
void Update(int i)
{
BeginInvoke((Action)delegate ()
{
dataGridView1[4, i].Value = "heheh";
});
}
So I have code longer then this which update datagrid and take data from datagrid, and sometimes work without crash but sometimes it hit this issue and not update properly, or push errors (out of index), but I think invoke prevent push errors, it will just skip the value I want add or take.. but finally it will crash program or wont work properly. Anyone have idea how to put to queque refresh or taking data, and wait till if finish refresh gui?