新手问题

kevinyang315 2012-03-08 09:30:22
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Thread G_th;
Random random = new Random();
int int_num;
private void button1_Click(object sender, EventArgs e)
{
RemoveControl();
int int_x = 10;
int int_y = 60;
for (int i = 0; i < 100; i++)
{
Button bt = new Button();
bt.Text = (i + 1).ToString();
bt.Name = (i + 1).ToString();
bt.Width = 35;
bt.Height = 35;
bt.Location = new Point(int_x, int_y);
bt.Click += new EventHandler(bt_Click);
int_x += 36;
if ((i + 1) % 10 == 0)
{
int_x = 10;
int_y += 36;
}
Controls.Add(bt);
}
G_th = new System.Threading.Thread(
delegate()
{
int int_Count = 0;
while (true)
{
int_Count = ++int_Count > 100000000 ? 0 : int_Count;
this.Invoke((MethodInvoker)delegate
{
label1.Text = int_Count.ToString();
});

System.Threading.Thread.Sleep(1000);
}
});
G_th.IsBackground = true;
G_th.Start();
int_num = random.Next(1, 100);
button1.Enabled = false;
}
void RemoveControl()
{
for (int i = 0; i < 100; i++)
{
if (Controls.ContainsKey((i + 1).ToString()))
{
for (int j = 0; j < Controls.Count; j++)
{
if (Controls[j].Name == (i + 1).ToString())
{
Controls.RemoveAt(j);
break;
}
}
}
}
}
void bt_Click(object sender, EventArgs e)
{
Control P_control = sender as Control;
if (int.Parse(P_control.Name) > int_num)
{
P_control.BackColor = Color.Orange;
P_control.Enabled = false;
P_control.Text = "大";
}
else if (int.Parse(P_control.Name) < int_num)
{
P_control.BackColor = Color.Green;
P_control.Enabled = false;
P_control.Text = "小";
}
else
{
G_th.Abort();
MessageBox.Show(string.Format("恭喜你猜对了!共猜了{0}次 用时{1}秒",
GetCount(), label1.Text), "恭喜!");
button1.Enabled = true;
}
}
string GetCount()
{
int int_temp = 0;
foreach (Control c in Controls)
{
if (!c.Enabled)
{
int_temp++;
}
}
return int_temp.ToString();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
}
请问红色标记的地方是什么意思,尤其是线程那一段。请高手给点详细的解释,怎么理解这些语句
...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
白色的海 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 happy09li 的回复:]
bt.Click += new EventHandler(bt_Click);// 为创建的按钮添加click事件


第二个地方,对线程不是很熟,好像是创建一个线程,在线程里面 申明委托

Environment.Exit(0); //终止当前进程并为基础操作系统提供指定的退出代码。
[/Quote]

第一个和第三个地方都同意.

第二个地方虽然写的是一个委托,其实是一个用lemda写的匿名函数,其效果等于:

G_th = new System.Threading.Thread(new ThreadStart(DoWork));

void DoWork()
{
线程函数体;
}

委托内的代码就是线程函数体。

并且,楼上都没提到为什么要用this.invoke,这个在子线程操作主线程(UI线程)时是必须用到的。
因为UI上的所有控件都是在UI线程上工作,与UI线程是绑定的,其它线程无权访问,如果需要使用,则必须
使用invoke方法,提交代码段由UI线程去执行,从而使得子线程可以访问UI线程的控件。
还有一个BeginInvoke方法也是一样,Invoke是同步执行,而BeginInvoke是异步不止,具体参考msdn。
feixuyue 2012-03-08
  • 打赏
  • 举报
回复
G_th = new System.Threading.Thread(
delegate()
{
int int_Count = 0;
while (true)
{
int_Count = ++int_Count > 100000000 ? 0 : int_Count;
this.Invoke((MethodInvoker)delegate
{
label1.Text = int_Count.ToString();
});

新开一个线程,然后跨线程调用需要使用委托
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0); } }
关闭进程
熙风 2012-03-08
  • 打赏
  • 举报
回复
bt.Click += new EventHandler(bt_Click);// 为创建的按钮添加click事件


第二个地方,对线程不是很熟,好像是创建一个线程,在线程里面 申明委托

Environment.Exit(0); //终止当前进程并为基础操作系统提供指定的退出代码。

computerrt 2012-03-08
  • 打赏
  • 举报
回复
我是来学习的。顶一下先。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧