抽奖系统的停止按钮怎么写?请大家多多提建议
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();
}
}
}
我想做一个抽奖系统,但我是抽奖系统的停止按钮我不会写。
请看到的朋友多多提建议。