关闭

c# 线程操作实例

94人阅读 评论(0) 收藏 举报
分类:
namespace 线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 启动线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //Start()启动线程,告诉CPU可以执行我的任务了,具体何时执行,由CPU决定.
            //Abort()终止线程,终止完成后不能再Start()
            //Thread.Sleep(1),使当前线程停止一段时间运行.
            //Name 线程名
            //Thread.CurrentThread 获得当前的线程引用。

            this.label1.Text = "";
            this.label2.Text = "";
            Control.CheckForIllegalCrossThreadCalls = false;//取消程序交叉检查
        }

        private void button1_Click(object sender, EventArgs e)
        {
            text();
        }

        private void text()
        {
            for (int i = 0; i < 1000000; i++)
            {
                this.label1.Text = i.ToString();
            }
        }

        private void text2(object obj)
        {
            for (int i = 0; i < 1000000; i++)
            {
                this.label2.Text = obj.ToString();
            }
        }
        /// <summary>
        /// 线程调用无参函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(text);
            th.IsBackground = true;
            th.Start();
        }

        /// <summary>
        /// 线程执行的函数若要带参,参数类型必须是object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(text2);
            th.IsBackground = true;
            th.Start("线程调用了带参数函数");
        }

        private void playgame()
        {
            Random r = new Random();

            while (true)
            {
                this.label1.Text = r.Next(0, 10).ToString();
                this.label2.Text = r.Next(0, 10).ToString();
            }
        }

      

        private void button4_Click(object sender, EventArgs e)
        {
            playgame();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(playgame);
            th.IsBackground = true;
            th.Start();
        }


        bool b = false;
        private void playgame2()
        {
            Random r = new Random();

            while (b)
            {
                this.label1.Text = r.Next(0, 10).ToString();
                this.label2.Text = r.Next(0, 10).ToString();
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Random r = new Random();

            if (b == false)
            {
                b = true;
                button6.Text = "停止";
                Thread th = new Thread(playgame2);
                th.IsBackground = true;
                th.Start();
            }
            else
            {
                b = false;
                button6.Text = "新奖(2)";
            }
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10573次
    • 积分:258
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:8篇
    • 译文:0篇
    • 评论:0条
    文章分类