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:實(shí)例化委托并調(diào)用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:讓子線程執(zhí)行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 閱讀(430) 評論(0)  編輯  收藏 所屬分類: .Net
          主站蜘蛛池模板: 金门县| 于田县| 廊坊市| 临泽县| 陵川县| 长治市| 织金县| 惠来县| 玉环县| 苏尼特右旗| 孟州市| 临朐县| 江永县| 淄博市| 通海县| 康平县| 辽阳市| 清苑县| 孝昌县| 广丰县| 万安县| 德化县| 永靖县| 雷山县| 壤塘县| 金溪县| 康马县| 博白县| 东阳市| 泾川县| 五寨县| 安仁县| 长宁区| 平武县| 盐山县| 五大连池市| 晋江市| 十堰市| 体育| 和静县| 稷山县|