先举个例子  private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= 20; i++)
            {
                Thread.Sleep(1000);
                label1.Text = i.ToString();
                
            }
        }     这样写界面会卡住,根本看不到每次lable加1的效果,而且点了这个按钮后界面不能移动,也不能进行其他操作     请问这个问题怎么解决啊

解决方案 »

  1.   

      private void button1_Click(object sender, EventArgs e)
      {
      for (int i = 0; i <= 20; i++)
      {
        for (int j = 0; j < 100; j++)
        {
             Thread.Sleep(10);
             Application.DoEvents();
        }
        label1.Text = i.ToString();
        
      }
      }
      

  2.   

    参考,学习一下
    http://www.cnblogs.com/jiajiayuan/articles/2075136.html
      

  3.   

    private void button1_Click(object sender, EventArgs e)
      {
      for (int i = 0; i <= 20; i++)
      {
      for (int j = 0; j < 1000; j++)
      {
      Thread.Sleep(1);
      Application.DoEvents();
      }
      label1.Text = i.ToString();
        
      }
      }
      

  4.   

    线程+委托 private void button1_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(Test));//用线程
                th.Start();
             }
           
            public void Test()
            {
                for (int i = 0; i <= 20; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        ViewLable(i.ToString()+"-"+j.ToString());
                    }
                }
            }
            public delegate void ViewLableDelegate(string str);//定义一个委托
            public void ViewLable(string str)
            {
                if (label1.InvokeRequired)
                {
                    WriMsgDelegate wmd = new WriMsgDelegate(ViewLable);
                    label1.Invoke(wmd, new string[] { str });
                }
                else
                {
                    label1.Text = str;
                }
             }
      

  5.   


     private void button1_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(Test));
                th.Start();
             }
           
            public void Test()
            {
                for (int i = 0; i <= 20; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        ViewLable(i.ToString()+"-"+j.ToString());
                    }
                }
            }
            public delegate void ViewLableDelegate(string str);//定义一个委托
            public void ViewLable(string str)
            {
                if (label1.InvokeRequired)
                {
                    ViewLableDelegate wmd = new ViewLableDelegate(ViewLable);//应该是这样,楼上是我代码复制过来的,不好意思
                    label1.Invoke(wmd, new string[] { str });
                }
                else
                {
                    label1.Text = str;
                }
             }