标签:style color io os ar div 问题 sp cti
最常见的情况就是把其它线程的文字加到listbox,总结了三种写法,由繁到简
1.普通青年:声明委托,调用委托,委托里调方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
delegate void AddListItemHandler(string
str);private void SetText
(string obj) { if (this.listBox1.InvokeRequired) { this.listBox1.Invoke(new AddListItemHandler(this.AddListItem),obj); } else { this.listBox1.Items.Add(obj); } }private void AddListItem
(string str){ this.listBox1.Items.Add(str);} |
2.文艺青年:用Action
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
private void SetText
(string obj){ if (this.listBox1.InvokeRequired) { this.Invoke(new Action<string>(this.AddListItem),obj); } else { this.listBox1.Items.Add(obj); }}private void AddListItem
(string str){ this.listBox1.Items.Add(str);} |
3.二逼青年:Action+Lambda,最简略,最优美
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private void SetText
(string obj){ if (this.listBox1.InvokeRequired) { this.Invoke(new Action<string>((x)
=> this.listBox1.Items.Add(x)),
obj); } else { this.listBox1.Items.Add(obj); }} |
标签:style color io os ar div 问题 sp cti
原文地址:http://blog.csdn.net/mono1977/article/details/39240245