0

I am facing an issue with updating DataGridView while each thread operates on different row, it all time crash.

Here is example of what I try to do, but honestly I don't know how to fix it.

Researched invoke method but doesn't work :(

private void threadingFunction()
{
    int ThreadsNumber = dataGridView1.Rows.Count;
    List<Task> tasks = new List<Task>();
    for (int i = 0; i < ThreadsNumber; i++)
    {
        tasks.Add(Task.Factory.StartNew(() => myFunction(i)));
        Thread.Sleep(1000);
    }
    Task.WaitAll(tasks.ToArray());
}

private void myFunction(int i)
{
    doSomething()
    dataGridView1[4, i].Value = "1";
    doSomething();
    dataGridView1[4, i].Value = "2";
    doSomething();
    Thread.Sleep(1000);
    dataGridView1[4, i].Value = "3";
    doSomething();
    Thread.Sleep(1000);
    dataGridView1[4, i].Value = "4";
    doSomething();
    Thread.Sleep(1000);
    dataGridView1[4, i].Value = "5";
    doSomething();
    Thread.Sleep(1000);
    dataGridView1[4, i].Value = "6";
}

Update

I figured out, when in same time thread1 update datagridview, and thread2 started same time want do same action take or fill datagrid view cell, it throw errors, "out of index", "error instance of object" etc. Anyone know how to fix this issue, because it crash my whole program.. and cant figure out how to correctly invoke implement to this .

here code I am testing:

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(() => myFunction(i)));
                Thread.Sleep(0);
            }
            Task.WaitAll(tasks.ToArray());
        }

        private void myFunction(int i)
        {

            var geg = dataGridView1[4, i].Value.ToString();
            dataGridView1[5, i].Value = geg;

            //dataGridView1[4, i].Value = "2";


            //dataGridView1[4, i].Value = "3";


            //dataGridView1[4, i].Value = "4";


            //dataGridView1[4, i].Value = "5";


            //dataGridView1[4, i].Value = "6";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            backgroundWorker1.RunWorkerAsync();
        }
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Do you get an Exception when it crashes? – Broots Waymb Apr 28 '21 at 12:43
  • 1
    What do you mean invoke doesn't work? What did you try exactly? Accessing UI components from other threads will not work directly. – György Kőszeg Apr 28 '21 at 12:44
  • 1
    Invoke should solve your cross threading issue. What error are you getting. – jdweng Apr 28 '21 at 12:55
  • I get this error https://prnt.sc/12799zo and this https://prnt.sc/1279br9 and Im sure row exist and not out of range, because half time work fine till it crash. – Adult Marketing Apr 28 '21 at 13:03
  • 1
    You'd better set `Control.CheckForIllegalCrossThreadCalls = true`. It protects you from shooting yourself in the foot. – Theodor Zoulias Apr 28 '21 at 22:00
  • this doesnt work sadly Ive tested it – Adult Marketing Apr 28 '21 at 22:32
  • 1
    @AdultMarketing - You cannot access or update a UI element from any thread other than the one that created it. That thread is usually the one and only UI thread. You can do background processing on other threads, but you can't touch any UI control in the background. – Enigmativity Apr 28 '21 at 22:59
  • @AdultMarketing multithreading is difficult. Unless you have at least a basic understanding of the concepts, you are advised to avoid it. In case you want to gain this understanding, here is a great online resource: [Threading in C#](http://www.albahari.com/threading/) by Joseph Albahari. – Theodor Zoulias Apr 29 '21 at 00:09

0 Answers0