线程(2)

Posted on 2019-12-11 14:10  冰糖Luck1996  阅读(101)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//引用线程
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace theard
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //跨线程访问控件
        private void Button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(()=>
            {
                for (int i = 0; i < 11; i++)
                {
                    if (label1.InvokeRequired)
                    {
                       label1.Invoke(new Action<string>(a=> { this.label1.Text = a; }),
                       i.ToString());
                    }
                    Thread.Sleep(1000);
                }

            });
            thread.IsBackground = true;
            thread.Start();
        }
    }
}
View Code