我又来了,关于线程里调用System.Thread.Timer计时器,但是结束线程,程序却还在运行.今天都用掉300分了,帮帮我,解决了在送分

sun900 2005-08-05 04:27:53
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace MyThread
{

public class TestThread
{
private System.Windows.Forms.Label label1;
private System.Threading.Thread timeThread;
public TestThread()
{
}
private void RunThread()
{
//这里开始线程
timeThread = new Thread(new ThreadStart(TestThreadTimer));
timeThread.Start();
}
private void StopThread()
{
//这里结束线程
timeThread.Abort();
//但是不能结束
}
public void TestThreadTimer()
{
System.Threading.Timer timer = new System.Threading.Timer(
new TimerCallback(this.TimerMethod),
null,
0,
500
);

}
public void TimerMethod(object state)
{
this.label1.Text = DateTime.Now.ToString();
}
}
}

//调用的timerThread不能结束,要用到线程池吗?
//线程里调用线程,如果强制结束调用线程,里面哪个线程也会结束吗?
...全文
688 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
superhasty 2006-01-08
  • 打赏
  • 举报
回复
是啊,如何强制结束线程呢?类似于PostThreadMessage
qpl007 2005-08-05
  • 打赏
  • 举报
回复
try:

namespace MyThread
{
public class TestThread
{
private bool _falg = false;
private System.Windows.Forms.Label label1;
private System.Threading.Thread timeThread;
public TestThread()
{
}
private void RunThread()
{
//这里开始线程
this._flag = true;
timeThread = new Thread(new ThreadStart(TestThreadTimer));
timeThread.Start();
}
private void StopThread()
{
this._flag = false;
if(timeThread.ThreadState != ThreadState.Running)
timeThread.Start();
timeThread.Abort();
}
public void TestThreadTimer()
{
while(this._flag)
{
this.label1.Text = DateTime.Now.ToString();
System.Threading.Thread.Sleep(500);
}
}
}
}


nik_Amis 2005-08-05
  • 打赏
  • 举报
回复
up
sun900 2005-08-05
  • 打赏
  • 举报
回复
用这个timeThread.IsBackground = true;
调用timeThread.Abort();
也不能终止线程
/////////

wangsaokui(无间道III(MSMVP)) ( ) 信誉:134

timer.Dispose();
timer=null;
---------
你说的这个对这段程序可以用,最终还是终止timer了,但是线程还是没有终止,其实我想问的是强制终止线程
好像用WIN32API里的PostThreadMessage消息可以强制终止线程
现在我在找怎么得到timeThread的线程ID

alang_jj 2005-08-05
  • 打赏
  • 举报
回复
线程一般是自动结束的
timeThread.IsBackground = true;
timeThread.Start();
不需要手动结束线程,这样关闭程序在进程里面就不会有你的程序了
wangsaokui 2005-08-05
  • 打赏
  • 举报
回复
或者
namespace MyThread
{

public class TestThread
{
private System.Windows.Forms.Label label1;
private System.Threading.Thread timeThread;
private System.Threading.Timer timer=null;
public TestThread()
{
}
private void RunThread()
{
//这里开始线程
timeThread = new Thread(new ThreadStart(TestThreadTimer));
timeThread.Start();
}
private void StopThread()
{
//这里结束线程
timer.Dispose();
timer=null;
timeThread.Abort();
//但是不能结束
}
public void TestThreadTimer()
{
timer = new System.Threading.Timer(
new TimerCallback(this.TimerMethod),
null,
0,
500
);

}
public void TimerMethod(object state)
{
this.label1.Text = DateTime.Now.ToString();
}
}
}
wangsaokui 2005-08-05
  • 打赏
  • 举报
回复
或者
namespace MyThread
{

public class TestThread
{
private System.Windows.Forms.Label label1;
private System.Threading.Thread timeThread;
private System.Threading.Timer timer=null;
public TestThread()
{
}
private void RunThread()
{
//这里开始线程
timeThread = new Thread(new ThreadStart(TestThreadTimer));
timeThread.Start();
}
private void StopThread()
{
//这里结束线程
timer.Dispose();
timer=null;
timeThread.Abort();
//但是不能结束
}
public void TestThreadTimer()
{
timer = new System.Threading.Timer(
new TimerCallback(this.TimerMethod),
null,
0,
500
);

}
public void TimerMethod(object state)
{
this.label1.Text = DateTime.Now.ToString();
}
}
}
alang_jj 2005-08-05
  • 打赏
  • 举报
回复
timeThread.IsBackground = true;
timeThread.Start();
wangsaokui 2005-08-05
  • 打赏
  • 举报
回复
Sorry,
private static bool thread_flag=true;
wangsaokui 2005-08-05
  • 打赏
  • 举报
回复
namespace MyThread
{

public class TestThread
{
private static thread_flag=true;
private System.Windows.Forms.Label label1;
private System.Threading.Thread timeThread;
public TestThread()
{
}
private void RunThread()
{
//这里开始线程
timeThread = new Thread(new ThreadStart(TestThreadTimer));
timeThread.Start();
}
private void StopThread()
{
//这里结束线程
//timeThread.Abort();
thread_flag=false;
//但是不能结束
}
public void TestThreadTimer()
{
while(thread_flag)
{
this.label1.Text = DateTime.Now.ToString();
Thread.Sleep(500);
}

}
}
}
zzczh 2005-08-05
  • 打赏
  • 举报
回复
Tony_lau111082 2005-08-05
  • 打赏
  • 举报
回复
必须先设置线程为后台线程,才能关闭。

timeThread.IsBackground = true;
haoztao 2005-08-05
  • 打赏
  • 举报
回复
偶不懂,帮你矿顶
3tzjq 2005-08-05
  • 打赏
  • 举报
回复
为了 实时时钟 不用弄得这么复杂吧?

private System.Windows.Forms.Timer timer1 = new Timer();//标准计时器
timer1 Interval = 1000;//一秒钟执行一次
timer1.Tick +=new EventHandler(timer1_Tick);//执行事件

private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = DateTime.Now.ToString();
}

110,536

社区成员

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

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

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