码迷,mamicode.com
首页 > 编程语言 > 详细

C# 摇奖机实例(线程)

时间:2014-09-18 23:34:04      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   art   

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;

namespace yaojiangji
{
    public partial class Form1 : Form
    {
        List<Label> lbList = new List<Label>();

        bool isCreate = false;

        public Form1()
        {
            InitializeComponent();

            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (isCreate)
            {
                isCreate = false;
                this.btnStart.Text = "开始";
            }
            else
            {
                isCreate = true;
                this.btnStart.Text = "结束";

                //方法一:

                /*
                new Thread(
                        ()=>
                        
                        {
                            Random random = new Random();

                            while (isCreate)
                            {

                                for (int i = 0; i < 6; i++)
                                {

                                    lbList[i].Text = random.Next(1, 10).ToString();

                                }

                                Thread.Sleep(200);
                            }
                        }

                    ).Start();
                 */



                //方法二:


                Thread thread = new Thread(new ThreadStart(start));

                //设置后台线程
                thread.IsBackground = true;

                thread.Start();

            
            }



        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 6; i++)
            {
                Label label = new Label();
                label.Text = i.ToString();
                label.AutoSize = true;
                label.Location = new Point(50*i+15,50);

                lbList.Add(label);
                this.Controls.Add(label);
            }
        }



        public void start()
        {

            Random random = new Random();

            while (isCreate)
            {
                for (int i = 0; i < 6; i++)
                {

                    lbList[i].Text = random.Next(1, 10).ToString();

                }

                Thread.Sleep(200);
            }
        }
    }
}

 

C# 摇奖机实例(线程)

标签:style   blog   color   io   os   ar   for   div   art   

原文地址:http://www.cnblogs.com/zoro-zero/p/3980259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!