multithreading - How to populate two dataGridViews continously using different threads in C#? -
i have created 2 datagridviews , start button shown in figrue.
when press start button 2 threads t1 , t2 created using delegate mydel.
actually, want both datagridview1 , datagridview2 populated continuously. datagridview2 getting updated continuously. after completion of datagridview2, datagridview1 starts updating.
can please give me suggestion how fix this?
using system; using system.windows.forms; using system.threading; namespace multithreadeddatagridviewdemo { public partial class form1 : form { private delegate void mydel(); int count=0; int count_1 = 0; public form1() { initializecomponent(); //cells created in datagridview1 , datagridview2 (int p = 0; p < 37; p++) { datagridview1.columns.add("", "");// dynamic cloumn adding datagridview1.rows.add();//dynamic row adding datagridview1.columns[p].sortmode = datagridviewcolumnsortmode.notsortable;//disables column header sorting datagridview1.columns[p].autosizemode = datagridviewautosizecolumnmode.none; datagridview1.columns[p].width = 70; datagridview2.columns.add("", "");// dynamic cloumn adding datagridview2.rows.add();//dynamic row adding datagridview2.columns[p].sortmode = datagridviewcolumnsortmode.notsortable;//disables column header sorting datagridview2.columns[p].autosizemode = datagridviewautosizecolumnmode.none; datagridview2.columns[p].width = 70; } } //method populate datagridview1 public void method1() { while (count < 10000) { (int = 0; < datagridview1.rows.count; i++) { (int k = 0; k < datagridview1.columns.count; k++) { datagridview1.rows[i].cells[k].value = count; } } count++; } } //method populate datagridview2 public void method2() { while (count_1 < 10000) { (int = 0; < datagridview2.rows.count; i++) { (int k = 0; k < datagridview2.columns.count; k++) { datagridview2.rows[i].cells[k].value = count_1; } } count_1++; } } //creates threads populate datagridview1 , datagridview1 private void btn_start_click(object sender, eventargs e) { mydel del_1 = new mydel(method1); thread t1 = new thread(new threadstart(del_1)); t1.start(); mydel del_2 = new mydel(method2); thread t2 = new thread(new threadstart(del_2)); t2.start(); } } }
you should use backgroundworker
, , update ui in it's progresschanged
-event, used access ui
-thread. reportprogress
-method can send int percentprogress
, object userstate
linked progresschanged
-method. userstate
-object can, example, pass list full of datagridviewrows
, add them in progresschanged
-method datagridview
.
Comments
Post a Comment