multithreading - Thread safe C# not working -
when run following code runs fine, not expected, assumed run on secure thread, components frozen until finishes running thread, not supposed run on new thread can use other controls?
using system; using system.drawing; using system.windows.forms; using system.threading; public class myformcontrol : form { public delegate void addlistitem(); public addlistitem mydelegate; private button mybutton; private thread mythread; private listbox mylistbox; public myformcontrol() { mybutton = new button(); mylistbox = new listbox(); mybutton.location = new point(72, 160); mybutton.size = new size(152, 32); mybutton.tabindex = 1; mybutton.text = "add items in list box"; mybutton.click += new eventhandler(button_click); mylistbox.location = new point(48, 32); mylistbox.name = "mylistbox"; mylistbox.size = new size(200, 95); mylistbox.tabindex = 2; clientsize = new size(292, 273); controls.addrange(new control[] {mylistbox,mybutton}); text = " 'control_invoke' example"; mydelegate = new addlistitem(addlistitemmethod); } static void main() { myformcontrol myform = new myformcontrol(); myform.showdialog(); } public void addlistitemmethod() { string myitem; for(int i=1;i<6;i++) { myitem = "mylistitem" + i.tostring(); mylistbox.items.add(myitem); mylistbox.update(); thread.sleep(300); } } private void button_click(object sender, eventargs e) { mythread = new thread(new threadstart(threadfunction)); mythread.start(); } private void threadfunction() { mythreadclass mythreadclassobject = new mythreadclass(this); mythreadclassobject.run(); } } // following code assumes 'listbox' , 'button' control added form, // containing delegate encapsulates method adds items listbox. public class mythreadclass { myformcontrol myformcontrol1; public mythreadclass(myformcontrol myform) { myformcontrol1 = myform; } public void run() { // execute specified delegate on thread owns // 'myformcontrol1' control's underlying window handle. myformcontrol1.invoke(myformcontrol1.mydelegate); } }
it assumed run on secure thread, components frozen until finishes running thread
when invoking delegate on control, delegate run on ui thread. i.e. code run on ui thread:
public void addlistitemmethod() { string myitem; for(int i=1;i<6;i++) { myitem = "mylistitem" + i.tostring(); mylistbox.items.add(myitem); mylistbox.update(); thread.sleep(300); // freeze ui thread } }
it not supposed run on new thread can use other controls?
you cannot use controls non-ui threads.
purpose of using background threads long-running operations not related ui. e.g. can read file disk, query api, or can run long-running calculation (n-th fibonacci number). if you'll run these kind of things on ui thread, application freeze. should run such operations on non-ui thread , return ui after have finished long-running operation (though can notify user progress of long-running operation).
if want periodically ui consider using system.windows.forms.timer
component. set timer interval 300 , add tick event handler:
private void button_click(object sender, eventargs e) { timer.start(); } private void timer_tick(object sender, eventargs e) { mylistbox.items.add($"mylistitem{mylistbox.items.count + 1}"); }
Comments
Post a Comment