multithreading - updating datagridview from within a thread causes error c# winform -
i writing c# winform uses datagridview object. @ point in program need update said datagridview within thread. causes program throw unhandled exception saying object reference not set instance of object, , makes datagridview turn error image of white box red x in it.
specifically trying in case update values in rows, removing rows , updating statistical values , reinserting rows.
i in separate function thread calls , updates 2 rows in datagridview every time.
here code update function:
private void updatestats(int binnumber) { binnumber *= 2;//has multiply 2 because every bin has 2 rows in table if (statsdata.rows.count >= (binnumber + 1)) { statsdata.rows.removeat(binnumber); //number not change because index decreased 1 statsdata.rows.removeat(binnumber);//because every bin requires 2 rows } bin bin = bins[binnumber / 2]; list<double> realetempdata = new list<double>(); list<double> imagetempdata = new list<double>(); list<double> realalphatempdata = new list<double>(); list<double> imagalphatempdata = new list<double>(); //updates average , std dev values foreach (minsearchdata datapoint in bin.bindata) { realetempdata.add(datapoint.reale); imagetempdata.add(datapoint.image); realalphatempdata.add(datapoint.realalpha); imagalphatempdata.add(datapoint.imagalpha); } bin.averagereale = realetempdata.average(); bin.averageimage = imagetempdata.average(); bin.stddevreale = calculatestandarddeviation(realetempdata); bin.stddevimage = calculatestandarddeviation(imagetempdata); bin.averagerealalpha = realalphatempdata.average(); bin.averageimagalpha = imagalphatempdata.average(); bin.stddevrealalpha = calculatestandarddeviation(realalphatempdata); bin.stddevimagalpha = calculatestandarddeviation(imagalphatempdata); realetempdata.clear(); imagetempdata.clear(); realalphatempdata.clear(); imagalphatempdata.clear(); datarow myrow = statsdata.newrow(); myrow[0] = bin.binname; myrow[1] = "real"; myrow[2] = bin.averagerealalpha; myrow[3] = bin.stddevrealalpha; myrow[4] = bin.averagereale; myrow[5] = bin.stddevreale; statsdata.rows.insertat(myrow, binnumber); datarow myrow2 = statsdata.newrow(); myrow2[0] = ""; myrow2[1] = "imaginary"; myrow2[2] = bin.averageimagalpha; myrow2[3] = bin.stddevimagalpha; myrow2[4] = bin.averageimage; myrow2[5] = bin.stddevimage; statsdata.rows.insertat(myrow2, binnumber + 1); }
the stranger part of seem unable catch exception, , happens inconsistently, in sometime fails first time tries update, , other times fails on first try.
any appreciated,
thanks,
-jake
this may cause due race
problem. in other words, 1 thread (likely main thread) tries paint grid based on current list of rows while other thread infers , manipulates it. moreover, guess have turned off checkforillegalcrossthreadcalls
able manipulate grid directly on threads. if is, main cause of problem. anyway, possible solution use begininvoke
indirectly work control:
private void updatestats(int binnumber) { datagridview1.begininvoke(new methodinvoker(() => { binnumber *= 2;//has multiply 2 because every bin has 2 rows in table if (statsdata.rows.count >= (binnumber + 1)) .... .... .... myrow2[5] = bin.stddevimage; statsdata.rows.insertat(myrow2, binnumber + 1); } }
edit:
i thought statsdata
datagridview
. based on op's comment datatable
. so, tailored answer reflect fact.
Comments
Post a Comment