标题:抽奖系统的停止按钮怎么写?请大家多多提建议
只看楼主
MAW2013
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-8-8
结帖率:0
已结贴  问题点数:20 回复次数:3 
抽奖系统的停止按钮怎么写?请大家多多提建议
namespace 多线程_抽奖
{
    public partial class Form1 : Form
    {
      
        int curGiftId = Convert.ToInt16(SqlHelper.ExecuteScalar("select max(giftid) from T_Gift"));
        Thread th = null;
        public Form1()
        {
            
            InitializeComponent();
           int k= lblLucknum.Width;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; //隐藏边框
            dg = new DeShowMessageInLabel(ShowMessageInLabel);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //开启线程间调用
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        //产生随机数
        public int GetRandom()
        {
            Random ra= new Random();
            int iUp = Convert.ToInt32(SqlHelper.ExecuteScalar("select max(Id) from T_User "));
            int iDown = Convert.ToInt32(SqlHelper.ExecuteScalar("select min(Id) from T_User "));
            int tem= ra.Next(iDown, iUp);
            return tem;
        }
        //获取身份证号
        public string GetLuckerIdcard(int userid)
        {
            return Convert.ToString(SqlHelper.ExecuteScalar("select Idcard from T_User where Id=" + userid));
        }
        //获取姓名
        public string GetLuckerName(int userid)
        {
            return Convert.ToString(SqlHelper.ExecuteScalar("select Name from T_User where Id=" + userid));
        }

        //获取奖品名称
        public string GetGiftName(int giftid)
        {
            return Convert.ToString(SqlHelper.ExecuteScalar("select GiftName from T_Gift where GiftId=" + giftid));
        }
        //判断是否中过奖
        public bool IsLucker(int LuckerId)
        {
            string cmd = "select 1 from T_user A inner join T_Lucker B on A.Id=B.LuckerId where A.Id="+LuckerId;
            object o = SqlHelper.ExecuteScalar(cmd);
            if (o==null||o==DBNull.Value )
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        //Lab显示方法
        void ShowMessageInLabel(string msg)
        {
             lblLucknum.Text=msg;
        }
        //委托显示方法
        delegate void DeShowMessageInLabel( string mseg);
        DeShowMessageInLabel dg = null;
        //显示号码
        public void ShowNum()
        {
            int i = 100;
            int k = 500;
            while (true)
            {
                i--;
                int luckid = GetRandom();
                if (IsLucker(luckid))
                {
                    continue;
                }
                else
                {
                    string luckerworkid = GetLuckerIdcard(luckid).Trim();
                    string luckername = GetLuckerName(luckid).Trim();
                    string mesg = luckerworkid + " " + luckername;
                    this.Invoke(dg, mesg);
                    if (i < 100 && i >= 50)
                    {
                        Thread.Sleep(100-i);
                    }
                    else if (i < 50 && i > 46)
                    {
                        k = k + 400;
                        Thread.Sleep(k);
                    }
                    else
                    {
                        lblLucknum.Text = "恭喜:" + lblLucknum.Text;
                        string cmd = "insert into T_Lucker(LuckerId,LuckerWorkId,LuckerName,LuckerGiftId,GiftName,Dtime)";
                        cmd += "values(" + luckid + ",'" + luckerworkid + "','" + luckername + "'," + curGiftId + ",'" + GetGiftName(curGiftId) + "','" + DateTime.Now + "')";
                        SqlHelper.ExcutNonQuery(cmd);
                        curGiftId--;
                        th.Abort();
                    
                    }
                }   
            }
        }

        //产生闪烁效果
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(lblLucknum.ForeColor == System.Drawing.Color.Red)
            {
                lblLucknum.ForeColor = System.Drawing.Color.Yellow;
            }
            else
            {
                lblLucknum.ForeColor = System.Drawing.Color.Red;
            }
        }
        //控制闪烁效果
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (!th.IsAlive)
            {
                timer1.Start();
                btnStart.Enabled = true;
            }
            else
            {
                timer1.Stop();
                lblLucknum.Visible = true;
                btnStart.Enabled = false;
            }
        }


        private void 数据导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
            this.Close();
            Application.Exit();
        }
        //抽奖
        private void btnStart_Click(object sender, EventArgs e)
        {
            lblLucknum.ForeColor = System.Drawing.Color.Red;
            //lblGiftName.Text =GetGiftName(curGiftId);
            lblGiftName.Text = curGiftId.ToString() + "号奖品: " + GetGiftName(curGiftId);
            Thread th1 = new Thread(new ThreadStart(ShowNum));
            th = th1;
            th.Start();
            timer2.Start();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            Thread th2 = new Thread(new ThreadStart(ShowNum));
            th = th2;
            th.Abort();
            timer2.Stop();
        }     
    }
}


我想做一个抽奖系统,但我是抽奖系统的停止按钮我不会写。
请看到的朋友多多提建议。
搜索更多相关主题的帖子: public 多线程 null 
2013-08-08 14:31
可靠的李颖
Rank: 2
等 级:论坛游民
帖 子:20
专家分:24
注 册:2013-6-17
得分:7 
饿
2013-08-09 11:05
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:7 
没仔细看抽奖的原理,如果是像开彩票一样,比如13个号码,那全抽出来以后就停止了呀,如果是由停止按钮决定,
那你设定一个全局标识,用于退出ShowNum()方法的while(true),通过停止铵钮来改变标识,退出循环
2013-08-09 13:30
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
得分:7 
学习中...

你不勇敢,谁替你坚强。。。
2013-08-09 16:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-418798-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.399645 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved