证人 2008 粤语:使用C#多线程设计的电脑摇奖程序-程序开发-红黑联盟

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 03:09:26

使用C#多线程设计的电脑摇奖程序
 
 
文章录入:7747.Net    责任编辑:7747.Net  70 
 【字体:小 大】
 
简单的一个小程序,代码如下:

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 exe_thread1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //号码字符串
        string[] str = { "15131254154", "15251247858", "15651244567",
                         "15344547254", "1551247732",  "15661242345",
                         "15461237356", "15761247611", "15873457954",
                         "15571247357", "15071247430", "15571678004",
                         "15611247553" };

        //随机产生号码
        Random r = new Random();

        //记录字符串下标
        int i;

        //定义线程
        Thread myThread;

        //线程方法
        private void Thread()
        {
            while (true)
            {
                this.SetText();
            }
       
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //实例化线程
            myThread = new Thread(new ThreadStart (this.Thread));

            //开始线程
            myThread.Start();
         }

        //定义委托
        delegate void SetTextCallback();


        //委托的方法
        private void SetText()
        {
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);

                this.Invoke(d, new object[] { });
            }
            else
            {

                i = r.Next(str.Length );

                this.textBox1.Text =str [i ] ;
                   
               
            }
        }  

        private void button2_Click(object sender, EventArgs e)
        {

            //结束线程
            myThread.Abort();

            MessageBox.Show("恭喜您得奖了!");

        }
    }
}
 

摘自红色黑客联盟(www.7747.net) 原文:http://www.7747.net/kf/201007/52405.html