再多线程过程中,通常会有更新UI的需求。跨线程怎么保证程序安全呢?答案是委托
新建类:
public class Test
{
public delegate void doit(object sender,eventars s);
public event doit doo;
int ii=0;
public void run()
{
while(true)
{
ii++;
doo(this,new eventars (){I=ii});
}
}
}
public class eventars:EventArgs
{
private int i;
public int I{get{return i};SET{i=value}}
}
主窗体
Test t = new Test()
private void button1_click(object sender,EventArgs s)
{
tt.doo+=new Test.doit(re);
Task t=new Task(tt.run);
t.start();
}
public void re(object sender,eventars v)
{
this.invoke((Action)delegate {label1.text=v.I.Tostring();});
}