解决方案 »

  1.   

    Thread.Sleep,将当前线程阻塞。
    阻塞主线程,窗体要等到休眠结束后才响应,这个时候就出现卡死的现象。新启一个线程,对新线程Thread.Sleep,不会阻塞主线程,也就是不会出现窗体卡死的现象。
      

  2.   

    UI线程里不要sleep,否则会卡死
      

  3.   

    application.doevents
    循环,sleep设置短一些
      

  4.   


    发现了一个残酷的现实,在类库中,并没有 DoEvents 方法
      

  5.   

    类库添加对system.windows.forms.dll的引用
    using system.windows.forms;
      

  6.   

    网上刚刚搜的代码,不保证能用,我晚上想测试一下的
    //创建StopNtime类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    namespace 程序运行暂停器
    {
        class StopNtime
        {
            public StopNtime()
            {
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//,跨线程调用控件必加上去         
            }
            private int stopTime = 0;//暂停的时间
            ThreadStart myStart;
            Thread TheStop;
            public  readonly  object MyLockWord = new object();
       // 
            public   void stopWay(int stopTime)
            {
               
                this.stopTime = stopTime;
                myStart = new ThreadStart(this .ToStop );
                TheStop = new Thread(myStart );
                TheStop.Start();
            }
            private   void ToStop()
            {
                lock (MyLockWord)
                {
              
                    Thread.Sleep(this .stopTime );
                    Thread.CurrentThread.Abort();
     
                }
               
            }
        }
    }//主窗体代码
    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 ;
    using System.Collections;
    namespace 程序运行暂停器
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }    
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = "";
                Thread f = new Thread(f1 );
                f.Start();
     
            }     
            string MyWord = "问题:1+1=? ...\n答案是:2 ...";
            private void f1()
            {
                StopNtime mmm = new StopNtime();
                foreach (char m in MyWord)
                {
                    lock (mmm.MyLockWord )
                    {
                      
                        label1.Text += m.ToString();                              
                    }
                    mmm.stopWay(300);            
                } 
            }
        }
    }
      

  7.   

    主线程sleep可定假死啊,做个后台线程,专门处理业务,前台的UI只显示数据就可以了
      

  8.   

    让你的线程等待mm毫秒,期间不会造成程序界面卡死。
    把原来调用Thread.Sleep的地方调用这个就可以了
            public static void Delay(int mm)
            {
                DateTime current = DateTime.Now;
                while (current.AddMilliseconds(mm) > DateTime.Now)
                {
                    Application.DoEvents();
                }
                return;
            }