求从线程执行一个很长的Thread.Sleep()UI线程又不卡死的办法,该怎么处理

求从线程执行一个很长的Thread.Sleep()UI线程又不卡死的办法
namespace WindowsFormsApplication1
{
    
   
    public partial class Form1 : Form
    {
        private delegate void MyDelegate();
        int num = 0;
        Thread t1;
        public Form1()
        {
            InitializeComponent();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            t1 = new Thread(SetText);
            t1.IsBackground = true;
            t1.Start();
        }
        
        private void SetText()
        {
            MyDelegate d1 = StartDelegate;
            while (true)
            {
                textBox1.Invoke(d1);
            }
        }
        void  StartDelegate()
        {
            //textBox1.Text = num++.ToString();
            Thread.Sleep(10000);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            t1.Suspend();
        }
     }
}

------解决方案--------------------
把sleep的地方改为

DateTime lastTime = DateTime.Now;
            DateTime currTime = lastTime;
            while ((currTime - lastTime).Ticks < 10000)
            {
                Application.DoEvents();
            }
------解决方案--------------------
之所以死,是因为
            while (true)
            {
                textBox1.Invoke(d1);
            }
在主线程写这个当然死。

另外
t1.Suspend();
不要这么写,停止或者挂起线程让线程自己做。
------解决方案--------------------
Thread.Sleep(10000); 写到while里面,而不要写到委托里面
写到委托里,委托是主线程去调用,相当于主线程sleep
------解决方案--------------------
用Timer吧
------解决方案--------------------
引用:
之所以死,是因为
            while (true)
            {
                textBox1.Invoke(d1);
            }
在主线程写这个当然死。

另外
t1.Suspend();
不要这么写,停止或者挂起线程让线程自己做。


纠正一下

 textBox1.Invoke(d1);  

这句是在主线程运行的,而不是

            while (true)
            {
                textBox1.Invoke(d1);
            }
在主线程与行的。
------解决方案--------------------
引用:
Thread.Sleep(10000); 写到while里面,而不要写到委托里面
写到委托里,委托是主线程去调用,相当于主线程sleep

真相