本文代码实现了WinForm界面显示实时更新,不会假死。程序界面如下:

代码如下:

using System;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart start = new ThreadStart(jisuan);
            new Thread(start).Start();
        }

        private void jisuan()
        {
            int i = int.Parse(textBox1.Text);
            int n = 0;
            for(int j=0;j<=i;j++)
            {
                Thread.Sleep(100);
                n = n + j;
                label2text(n.ToString());
            }
        }
        
        private void label2text(string str)
        {
            if (this.label2.InvokeRequired)
            {
                label2Invoke method = new label2Invoke(this.label2text);
                base.Invoke(method, new object[] { str });
            }
            else
            {
                this.label2.Text = str;
            }
        }

        private delegate void label2Invoke(string str);
    }
}