首页 新闻 会员 周边 捐助

自己动手写了一个摇奖机,但存在一个延迟问题,不知道延迟的原因是什么,希望高手能帮忙解答一下

0
[已解决问题] 解决于 2012-10-23 18:59
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 摇奖机
{
    public partial class Form1 : Form
    {

        delegate void GetNumDelegate(Label lb, int i);
        GetNumDelegate _getNum;
        public Form1()
        {
            InitializeComponent();
            _getNum = new GetNumDelegate(this.SetLbValue);
            //Label.CheckForIllegalCrossThreadCalls = false;
        }
        Thread startThread1, startThread2, startThread3, startThread4, startThread5, startThread6;
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (btnStart.Text == "开始摇")
            {
                try
                {
                    startThread1 = new Thread(GetNum);
                    startThread1.IsBackground = true;
                    startThread1.Start(lb1);

                    startThread2 = new Thread(GetNum);
                    startThread2.IsBackground = true;
                    startThread2.Start(lb2);

                    startThread3 = new Thread(GetNum);
                    startThread3.IsBackground = true;
                    startThread3.Start(lb3);

                    startThread4 = new Thread(GetNum);
                    startThread4.IsBackground = true;
                    startThread4.Start(lb4);

                    startThread5 = new Thread(GetNum);
                    startThread5.IsBackground = true;
                    startThread5.Start(lb5);

                    startThread6 = new Thread(GetNum);
                    startThread6.IsBackground = true;
                    startThread6.Start(lb6);
                    btnStart.Text = "停止";
                }
                catch (Exception)
                {

                    throw;
                }
            }
            else if (btnStart.Text == "停止")
            {
                string s1, s2, s3, s4, s5, s6;
                startThread1.Abort();
                s1= lb1.Text+" ";

                startThread2.Abort();
                s2= lb2.Text + " ";

                startThread3.Abort();
                s3= lb3.Text + " ";

                startThread4.Abort();
                s4= lb4.Text + " ";

                startThread5.Abort();
                s5= lb5.Text + " ";

                startThread6.Abort();
                s6= lb6.Text;
                btnStart.Text = "开始摇";
                lbResult.Text = "中奖结果为:"+s1+s2+s3+s4+s5+s6;

            }
        }
            Random ran = new Random();
        public void GetNum(Object lb)
        {
            Label lbt = lb as Label;
            while (true)
            {
                int i = ran.Next(10);
                this.Invoke(_getNum, lbt, i);
                Thread.Sleep(150);
            }
        }

        public void SetLbValue(Label lb, int i)
        {
            lb.Text = i.ToString();
        }
    }
}
AntColony的主页 AntColony | 菜鸟二级 | 园豆:215
提问于:2012-10-22 00:53
< >
分享
最佳答案
0
摇奖机
        public FrmErnie()
        {
            InitializeComponent();
            //不设置这个属性的话在后面就会报错
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        ////创建一个线程执行
        Thread t;
        //给定一个bool值判断是否执行还是停止
        bool isStartEnd = false;
        private void btnStart_Click(object sender, EventArgs e)
        {
            
            if (isStartEnd)
            {
                string result1, result2, result3, result4, result5, result6;
                //获取空间上面所得到的值
                result1 = lblResult.Text;
                result2 = lblResult1.Text;
                result3 = lblResult2.Text;
                result4 = lblResult3.Text;
                result5 = lblResult4.Text;
                result6 = lblResult5.Text;
                //判断当用户单击结束的时候显示的结果
                btnStart.Text = "开始";
                lblErnieResult.Text += result1 + result2 + result3 + result4 + result5 + result6;
                isStartEnd = false;
            }
            else
            {
                lblErnieResult.Text = "";
                t = new Thread(GetNum);
                t.IsBackground = true;
                t.Start();

                btnStart.Text = "结束";
                isStartEnd = true;
            }
        }

        //定义一个方法实现随机数的读取
         void GetNum()
        {
            //定义随机数
            Random rand = new Random();
            //生成一个随机数
            while (true)
            {
                if (isStartEnd)
                {
                    lblResult.Text = rand.Next(9).ToString();
                    lblResult1.Text = rand.Next(9).ToString();
                    lblResult2.Text = rand.Next(9).ToString();
                    lblResult3.Text = rand.Next(9).ToString();
                    lblResult4.Text = rand.Next(9).ToString();
                    lblResult5.Text = rand.Next(9).ToString();
                    Thread.Sleep(200);
                }
            }
        }
奖励园豆:5
Kencery | 菜鸟二级 |园豆:357 | 2012-10-22 12:34

你这个肯定不行,在方法内实例化一个random,这样会使产生的随机数一样,这是因为random是一个伪随机类

AntColony | 园豆:215 (菜鸟二级) | 2012-10-22 19:54

再细看了一下,各种错啊

AntColony | 园豆:215 (菜鸟二级) | 2012-10-22 19:56

@壹卄の➹: 什么错误呢???

Kencery | 园豆:357 (菜鸟二级) | 2012-10-22 23:49

@Kencery: 你的这种写法你要n创建多少个线程呢,要是写一千个数字呢?,一万甚至更多呢?难道你开启一千个线程??new Thread(GetNum)!!

Kencery | 园豆:357 (菜鸟二级) | 2012-10-22 23:52

@Kencery: 之前我只是看了一下你的代码,就直接判断了,没有对代码进行编译,是我的不对。

刚我运行了一下,你的代码存在一个问题:没有终止线程,当多点几次之后,占用过多cpu电脑变卡。于是我加了一个终止线程的代码:

 if (isStartEnd)
            {
               
string result1, result2, result3, result4, result5, result6;
               
//获取空间上面所得到的值
                result1 = lblResult.Text;
                result2
= lblResult1.Text;
                result3
= lblResult2.Text;
                result4
= lblResult3.Text;
                result5
= lblResult4.Text;
                result6
= lblResult5.Text;
               
//判断当用户单击结束的时候显示的结果
                btnStart.Text = "开始";
                lblErnieResult.Text
+= result1 + result2 + result3 + result4 + result5 + result6;
                isStartEnd
= false;

                t.Abort();//这个是我加进去的,用来终止线程

            }

在你的这段代码中我有一个地方不明白,就是你产生随机数的方法怎么就能产生不同的随机数,我之前也是用这种方法产生随机数的,6个随机数产生的结果是一样的,这个也是可以通过查msdn找到的,里面有一个是关于伪随机数的介绍,你看看就知道了,这也是我直接对你的代码一看就否定的原因

AntColony | 园豆:215 (菜鸟二级) | 2012-10-23 18:12

@壹卄の➹: 其实这样写线程是已经终止的,不需要再终止,因为我是用bool类型来判断的,所以,但是加上也行,下面的那个是因为我给每个控件  lblResult.Text = rand.Next(9).ToString();都执行可这个,所以他获取的信息不一样!!

Kencery | 园豆:357 (菜鸟二级) | 2012-10-23 21:46
其他回答(3)
0

Thread.Sleep(150)

 

程序中加了延时啊

田林九村 | 园豆:2367 (老鸟四级) | 2012-10-22 08:04

跟这个应该没有关系吧 这是让线程休息一下

支持(0) 反对(0) AntColony | 园豆:215 (菜鸟二级) | 2012-10-22 19:52
0

你说的是哪里存在延时,问题再具体些……

ms_water | 园豆:510 (小虾三级) | 2012-10-22 08:52

你看图片,就是抽奖的结果与摇出来的结果不一样

支持(0) 反对(0) AntColony | 园豆:215 (菜鸟二级) | 2012-10-22 19:53
0

 双色球中奖了

世界万物 | 园豆:276 (菜鸟二级) | 2012-10-22 10:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册