step1:定义委托
private delegate void loadingRun(int step);
step2:将对UI的操作封装成方法
private void doUpdate(int step)
{
if (step == 1)
{
this.checkBox1.Checked = true;
this.checkBox1.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox2.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 2)
{
this.checkBox2.Checked = true;
this.checkBox2.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox3.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 3)
{
this.checkBox3.Checked = true;
this.checkBox3.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox4.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 4)
{
this.checkBox4.Checked = true;
this.checkBox4.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.Close();
}
}
step3:实例化委托并调用doUpdate
public void run()
{
loadingRun load = new loadingRun(doUpdate);
//do something
this.Invoke(load, 1);
//do something
this.Invoke(load, 2);
//do something
this.Invoke(load, 3);
//do something
this.Invoke(load, 4);
}
step4:让子线程执行run
private void init()
{
ThreadStart ts = new ThreadStart(run);
Thread runThread = new Thread(ts);
runThread.Start();
}
========================================分割线=============以下为完整代码============================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class LoadingTest : Form
{
private delegate void loadingRun(int step);
public LoadingTest()
{
InitializeComponent();
init();
}
private void init()
{
ThreadStart ts = new ThreadStart(run);
Thread runThread = new Thread(ts);
runThread.Start();
}
public void run()
{
loadingRun load = new loadingRun(doUpdate);
//do something
this.Invoke(load, 1);
//do something
this.Invoke(load, 2);
//do something
this.Invoke(load, 3);
//do something
this.Invoke(load, 4);
}
private void doUpdate(int step)
{
if (step == 1)
{
this.checkBox1.Checked = true;
this.checkBox1.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox2.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 2)
{
this.checkBox2.Checked = true;
this.checkBox2.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox3.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 3)
{
this.checkBox3.Checked = true;
this.checkBox3.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.checkBox4.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
}
else if (step == 4)
{
this.checkBox4.Checked = true;
this.checkBox4.Font = new System.Drawing.Font("MS UI Gothic", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
this.Close();
}
}
}
}
posted on 2011-09-21 10:24
Ying-er 阅读(414)
评论(0) 编辑 收藏 所属分类:
.Net