c# - How can I report multiple reportprogress values from backgroundworker dowork event to a dataGridView? -


added class:

public class myprogress         {             public string id { get; set; }             public string progress { get; set; }         } 

the dowork event:

private void backgroundworker1_dowork(object sender, doworkeventargs e)         {                         backgroundworker worker = sender backgroundworker;              if ((worker.cancellationpending == true))             {                 e.cancel = true;             }             else             {                 list<intptr> intptrs = getprocessesintptrlist();                 (int x = 0; x < intptrs.count ; x++)                 {                     getprocessinfo(intptrs[x]);                 }                     while (true)                     {                         list<myprogress> prog = new list<myprogress>();                          prog = new list<myprogress>();                          proclist = process.getprocesses().tolist();                         (int = 0; < proclist.count; i++)                         {                             process[] processes = process.getprocessesbyname(proclist[i].processname);                             performancecounter performancecounter = new performancecounter();                             performancecounter.categoryname = "process";                             performancecounter.countername = "working set - private";//"working set";                             performancecounter.instancename = processes[0].processname;                              prog.add(new myprogress { id = proclist[i].processname, progress = ((uint)performancecounter.nextvalue() / 1024).tostring("n0") });                              worker.reportprogress(0, prog);                         }                     }             }         } 

and last backgroundworker progresschanged event want add values of each process datagridview1 rows under cell 3.

private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e)         {             foreach (myprogress p in (e.userstate list<myprogress>))             {                 currentrow.cells[3].value = p.progress;                 datagridview1.rows.add(                     p.progress);             }         } 

the variable currentrow not exist yet. , know cell 3 want add rows of processes values.

and don't know how many rows there should be. , how reporting of each process value row under cell 3 ?

i tried in progresschanged event:

private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e)         {             foreach (myprogress p in (e.userstate list<myprogress>))             {                 int rowidx = datagridview1.rows.add();                 datagridview1.rows[rowidx].cells[3].value = p.progress;             }         } 

but i'm getting exception on foreach: exception is:

additional information: collection modified; enumeration operation may not execute.  system.invalidoperationexception unhandled user code   hresult=-2146233079   message=collection modified; enumeration operation may not execute.   source=mscorlib   stacktrace:        @ system.collections.generic.list`1.enumerator.movenextrare()        @ automation.form1.backgroundworker1_progresschanged(object sender, progresschangedeventargs e) in d:\c-sharp\automation\automation\automation\form1.cs:line 719   innerexception:  

as far understand question, need

integer rowidx = datagridview1.rows.add(); datagridview1.rows[rowidx].cells[3].value = p.progress 

why error? - because have design flaw - in backgroundworker1_dowork, runs on background thread, still modifying collection, @ same time passing progress event. happens, while iterating on 1 thread, thread adds items. need make copy, pass copy , iterate copy. here 1 simple way array

 . . . .       myprogress[] arrp;      prog.copyto(arrp);      worker.reportprogress(0, arrp);  . . .    private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) {      myprogress[] progarr  = (myprogress[])e.userstate;     foreach (myprogress p in progarr)     {         int rowidx = datagridview1.rows.add();             datagridview1.rows[rowidx].cells[3].value = p.progress;     } } 

i not sure whole design, not trying change it. saying wrong now, not in general.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -