打开委托
private void Form4_Load(object sender, EventArgs e)
{
//创建Task委托
Task task = new Task(jihe);
//打开
task.Start();
}
创建实体
object obj=new object();
List<string> list = new List<string>();
委托任务
public void cehsi()
{
lock (obj)
{
for(int i = 0; i < list.Count; i++)
{
this.BeginInvoke(new Action(() => { label1.Text = i.ToString() + "%"; }));
this.BeginInvoke(new Action(() => { textBox1.AppendText(i.ToString()); }));
this.BeginInvoke(new Action(() => { textBox1.AppendText("\r\n"); }));
this.BeginInvoke(new Action(() => { progressBar1.Value = this.progressBar1.Value + 1; }));
Thread.Sleep(100);
}
string runStr = "结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
this.BeginInvoke(new Action(() => { this.textBox1.Text = this.textBox1.Text + runStr; }));
}
}