C # 2.0 Anonymity method and Windows Forms asynchronous call

xiaoxiao2021-03-06  67

C # 2.0 Anonymous method greatly simplifies the implementation of Windows Forms asynchronous calls, and we don't have to define asynchronous commission or packaging classes. For example, in the following code example, FORM1 has a button control and a list control, in the click event of the button control, we create a new thread, add 10 items to the list control in this thread:

Public class form1: system.windows.forms.form {private system.windows.forms.listbox listbox1; private system.windows.Forms.Button Button1;

...

Private void button1_click (object sender, evenetargs e) {thread thread = new thread (this.threadproc); thread.start ();

Private void threadProc () {for (int i = 0; i <10; i ) {this.invoke ((MethodInvoker) delegate {this.listbox1.items.add ("item" (i 1) .tostring () );}

转载请注明原文地址:https://www.9cbs.com/read-111021.html

New Post(0)