好开心啊~
抄人家的程式,抄了这么久,终于懂了委派了.
做了3个委派
一个只有方法
一个是方法(变数)
一个是方法(变数,变数)
一开始我们要先宣告委派,再来实作委派.
要用的时侯用beginkIvoke,再里面new 委派(再把实作的function放进去)
delegate void d1();//宣告委派 private void _d1()//实作委派 { textBox1.Text = "d1"; } private void button6_Click(object sender, EventArgs e) { this. BeginInvoke( //呼叫 new d1( //new 委派 _d1 //把实作function放进来 ) ); }
delegate void d2(string val); private void _d2(string val) { textBox1.Text +=Environment.NewLine+ val ; } private void button7_Click(object sender, EventArgs e) { this.BeginInvoke( new d2(_d2) //new委派,把实作function放进来 ,new object []{"d2"} //变数,new object[],把变数放进来 ); }
delegate void d3(Control ctr, string val); private void _d3(Control ctr, string val) { if (Ctrl is TextBox) { TextBox tx = (TextBox)ctr; tx.Text += Environment.NewLine+"d3:"+val; } } private void button8_Click(object sender, EventArgs e) { this.BeginInvoke(new d3(_d3),new object[]{textBox1,DateTime.Now.ToString()}); }
用到的地方
1.不同执行绪间的变数存取.e.g.执行绪A,想set执行绪B的lable.text