多线程加载进度条实例
-首先声明一个委托:
1 public delegate void LoadProgressDelegate(string msg,bool finish);//两个参数,一个参数传信息,另一个判断是否已经完成加载
2 public LoadProgressDelegate loadProgressDeleage;//定义一个委托(在程序里面再去实例化)
3
2 public LoadProgressDelegate loadProgressDeleage;//定义一个委托(在程序里面再去实例化)
3
实现委托方法:
1 private void LoadProgress(string msg, bool finish)
2 {
3 //eg:示例代码
4 this.Loading.lblTip.Text = msg;
5 this.Loading.Visible = true;
6 this.Loading.loadingCircle.Active = true;
7 if (finish)
8 {
9 gvPicture.RefreshAll();
10 this.Loading.Visible = false;
11 this.Loading.loadingCircle.Active = false;
12 this.Cursor = Cursors.Default;
13 }
14 }
2 {
3 //eg:示例代码
4 this.Loading.lblTip.Text = msg;
5 this.Loading.Visible = true;
6 this.Loading.loadingCircle.Active = true;
7 if (finish)
8 {
9 gvPicture.RefreshAll();
10 this.Loading.Visible = false;
11 this.Loading.loadingCircle.Active = false;
12 this.Cursor = Cursors.Default;
13 }
14 }
然后再写给加载数据和新建一个线程的方法,这里命名为:LoadingData();
1 private void LoadingData()
2 {
3 LoadSplashClass lsc = new LoadSplashClass(控件1,this);//this表示当前窗体
4 Thread thlsc = new Thread(new ThreadStart(lsc.LoadSplashDefine));
5 thlsc.Start();
6 }
2 {
3 LoadSplashClass lsc = new LoadSplashClass(控件1,this);//this表示当前窗体
4 Thread thlsc = new Thread(new ThreadStart(lsc.LoadSplashDefine));
5 thlsc.Start();
6 }
再接下来就实现上面LoadingData()里面的LoadSplashClass类:
1 class LoadSplashClass
2 {
3 控件1 ctl;//定义一个控件1:ctl
4 本窗体 form;//定义一个本窗体:form
5 public LoadSplashClass(控件1 control1,本窗体 form)
6 {
7 ctl = control1;
8 this.form = form;
9 }
10 public void LoadSplashDefine()
11 {
12 try
13 {
14 form.Invoke(form.loadProgressDelegate, new object[] { "正在加载数据,请稍等
", false });
15 加载内容; //eg: gvPic.BackGvObject = form.LoadWareHouseDefine();
16 System.Threading.Thread.Sleep(500);//测试用
17 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据完成!", false });
18 System.Threading.Thread.Sleep(500);//测试用
19 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据完成!", true });
20 }
21 catch(Exception ex)
22 {
23 string msg = ex.Message;
24 if (ex.InnerException != null)
25 {
26 msg += ex.InnerException.Message;
27 }
28 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据失败!" + msg, false });
29 System.Threading.Thread.Sleep(500);//测试用
30 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据失败!" + msg, true });
31 }
32 }
33 }
2 {
3 控件1 ctl;//定义一个控件1:ctl
4 本窗体 form;//定义一个本窗体:form
5 public LoadSplashClass(控件1 control1,本窗体 form)
6 {
7 ctl = control1;
8 this.form = form;
9 }
10 public void LoadSplashDefine()
11 {
12 try
13 {
14 form.Invoke(form.loadProgressDelegate, new object[] { "正在加载数据,请稍等
", false });15 加载内容; //eg: gvPic.BackGvObject = form.LoadWareHouseDefine();
16 System.Threading.Thread.Sleep(500);//测试用
17 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据完成!", false });
18 System.Threading.Thread.Sleep(500);//测试用
19 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据完成!", true });
20 }
21 catch(Exception ex)
22 {
23 string msg = ex.Message;
24 if (ex.InnerException != null)
25 {
26 msg += ex.InnerException.Message;
27 }
28 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据失败!" + msg, false });
29 System.Threading.Thread.Sleep(500);//测试用
30 form.Invoke(form.loadProgressDelegate, new object[] { "加载数据失败!" + msg, true });
31 }
32 }
33 }
在主窗体Load事件中实例化委托,以及执行LoadingData()方法;
1 loadProgressDelegate = new LoadProgressDelegate(LoadProgress);
2 LoadingData();
2 LoadingData();
一个多线程加载就算完成了。
浙公网安备 33010602011771号