深度优先

这个家伙好懒,除了文章什么都没留下

0%

【DIY】一个带线程的抽奖机

由于经常看到老师搞个随机数,来点人回答问题。 那个程序未免也太傻瓜式了,一点就产生了一个数,毫无悬念的就出来结果了。

就想让那个过程动起来,可以人为的控制开始和结束的过程。

也就是一个不断给lalble赋值的过程,于是就写了一个while的死循环,不点暂停,它就一直在给lalble赋值。

那么问题来了,這样不断赋值的过程就造成的窗体的假死现象。程序就死在那,暂停也没有用。

于是找了点资料。开启一个新的线程来执行这个命令就好了。

上图(觉得界面效果做得还不错)

代码其实也蛮简单的(一个选姓名的,一个选彩票的,可以自己设置候选项)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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 CCWin;
using System.Threading;
using System.Media;
using System.IO;

namespace 抽奖机
{
public partial class Form1 : CCSkinMain //Form//CCSkinMain
{
public Form1()
{
InitializeComponent();
}

Random R = new Random();//随机数

//加载
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;//禁止跨线程检查
this.TabPage.Parent = this.TabControl1;//显示page1
}

//按姓名随机选
bool a = false;
private void btnNameGo_Click(object sender, EventArgs e)
{
//music.SoundLocation = @"1.wav"; //路径,背景音乐
Thread th2 = new Thread(name);//创建一个线程(来调用name方法)
if (a == false)
{
th2.IsBackground = true;//设置为后台线程
th2.Start();//开启线程
btnNameGo.Text = "停 止";
//music.Play();//播放背景音乐
a = true;
}
else
{
a = false;
th2.Abort();//结束线程
btnNameGo.Text = "开 始";
//music.Stop();//停止播放
}
}
public void name()
{
while(a)
{
int x=R.Next(0, lbl.Items.Count);
lalN.Text = lbl.Items[x].ToString();
}
}

//福彩3D
bool b = false;
private void btnGo_Click_1(object sender, EventArgs e)
{
Thread th = new Thread(PlayGame);
if (b == false)
{
th.IsBackground = true;
btnGo.Text = "停 止";
th.Start();
b = true;
}
else
{
b = false;
th.Abort();
btnGo.Text = "开 始";
}
}
public void PlayGame()
{
while (b)
{
lalNum1.Text = R.Next(0, 10).ToString();
lalNum2.Text = R.Next(0, 10).ToString();
lalNum3.Text = R.Next(0, 10).ToString();
}
}

//背景音乐,去掉了
//SoundPlayer music = new SoundPlayer();


//添加学生姓名
private void btnTj_Click(object sender, EventArgs e)
{
if (txtName.Text=="")
{
MessageBox.Show("添加的姓名不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
return;
}
lbl.Items.Add(txtName.Text);
//MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}

//清空所有名单
private void btnQK_Click(object sender, EventArgs e)
{
if (MessageBox.Show("您确定要移除所有名单吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk)alogResult.Yes)
{
lbl.Items.Clear();
}
}
int i = -1;
private void lbl_SelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = lbl.Text;
i = lbl.SelectedIndex;
}

//移除一个人
private void btnCL_Click(object sender, EventArgs e)
{
if (i == -1)
{
MessageBox.Show("请先选中移除目标!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
lbl.Items.RemoveAt(i);
i = -1;
}

//图片的
bool c = false;
private void btnIMG_Click(object sender, EventArgs e)
{
Thread th3 = new Thread(PlayImg);
if (c == false)
{
th3.IsBackground = true;
btnIMG.Text = "停 止";
th3.Start();
c = true;
}
else
{
c = false;
th3.Abort();
btnIMG.Text = "开 始";
}
}
public void PlayImg()
{
int max = imageList1.Images.Count;
while (c)
{
int x1 = R.Next(0, lbl.Items.Count);
lalName1.Text = lbl.Items[x1].ToString();//图片的
int x2 = R.Next(0, lbl.Items.Count);
lalName2.Text = lbl.Items[x2].ToString();
}
}
}
}