关于两个button中调用线程对象的问题

yunhaiC QQ654777694 2009-04-21 04:17:27
private void button1_Click(object sender,MouseEventArgs e)
{
Thread th = new Thead(new ThreadStart(step_for));
th.Start();
}

void step_for()
{
for(int i=1;i<=20;i++)
{
........//省略
}
}

private void button2_MouseDown(object sender,MouseEventArgs e)
{
th.Abort(); //名称th不存在于目前内容中,请问怎么解决}
...全文
102 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcfy2 2009-04-22
  • 打赏
  • 举报
回复
一切要从事实说话,12L你UP你最好看一下后面的回复,你也自己去试试去
KerryMo 2009-04-21
  • 打赏
  • 举报
回复
学习
蓝海D鱼 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jdhlowforever 的回复:]
引用 4 楼 zgke 的回复:
private void button1_Click(object sender,MouseEventArgs e)
{
Thread th = new Thead(new ThreadStart(step_for));
th.Start();
}

改成

private Thread th = new Thead(new ThreadStart(step_for));
private void button1_Click(object sender,MouseEventArgs e)
{
th.Start();
}

同意!
[/Quote]up
abcfy2 2009-04-21
  • 打赏
  • 举报
回复
LS的可以解决,原来是这样搞定的啊。
Garnett_KG 2009-04-21
  • 打赏
  • 举报
回复


private Thread th = null;

private void button1_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(step_for));
th.Start();
}

private void button2_Click(object sender, EventArgs e)
{
if (th != null)
{
th.Abort();
}
}


abcfy2 2009-04-21
  • 打赏
  • 举报
回复
帮顶一下,写了半天也没写出来
  • 打赏
  • 举报
回复
up!!!
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zgke 的回复:]
private void button1_Click(object sender,MouseEventArgs e)
{
Thread th = new Thead(new ThreadStart(step_for));
th.Start();
}

改成

private Thread th = new Thead(new ThreadStart(step_for));
private void button1_Click(object sender,MouseEventArgs e)
{
th.Start();
}
[/Quote]

栏位初始设定式或无法参考非静态栏位,方法或属性'step_for()'
abcfy2 2009-04-21
  • 打赏
  • 举报
回复
那样绝对会提示当前上下文不存在step_for
jdhlowforever 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zgke 的回复:]
private void button1_Click(object sender,MouseEventArgs e)
{
Thread th = new Thead(new ThreadStart(step_for));
th.Start();
}

改成

private Thread th = new Thead(new ThreadStart(step_for));
private void button1_Click(object sender,MouseEventArgs e)
{
th.Start();
}
[/Quote]
同意!
zgke 2009-04-21
  • 打赏
  • 举报
回复
private void button1_Click(object sender,MouseEventArgs e)
{
Thread th = new Thead(new ThreadStart(step_for));
th.Start();
}

改成

private Thread th = new Thead(new ThreadStart(step_for));
private void button1_Click(object sender,MouseEventArgs e)
{
th.Start();
}
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abcfy2 的回复:]
th并不是在button2_MouseDown(object sender,MouseEventArgs e)中声明的,当然会抛出这个异常了
[/Quote]
所以我想问下怎么解决
如果在class开头new一个线程,那它又会报错,因为这个方法step_for()的问题
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abcfy2 的回复:]
th并不是在button2_MouseDown(object sender,MouseEventArgs e)中声明的,当然会抛出这个异常了
[/Quote]
所以我想问下怎么解决
如果在class开头new一个线程,那它又会报错,因为这个方法step_for()的问题
abcfy2 2009-04-21
  • 打赏
  • 举报
回复
th并不是在button2_MouseDown(object sender,MouseEventArgs e)中声明的,当然会抛出这个异常了

111,126

社区成员

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

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

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