zoukankan      html  css  js  c++  java
  • C# 摇奖机实例(线程)

    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);
                }
            }
        }
    }
  • 相关阅读:
    数据结构之排序查找算法
    Spring3之IOC
    SQL使用范例
    数据结构之链表操作
    VI的使用
    数据结构之树的操作
    Hibernate学习笔记
    Spring3之AOP
    01.由一个程序开始(一)
    Linux的档案权限及目录配置(一) (2)
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/3980259.html
Copyright © 2011-2022 走看看