定时器的辩论!

hetengfei_ 2012-01-01 03:25:26
听说。下面的代码,用到了 多线程定时器??

//
public System.Threading.Timer timer;
//
public MyThreadClass tClass;
//打开定时器
private void btnTimer1_Click(object sender, EventArgs e)
{
if (null == tClass)
{
tClass = new MyThreadClass();
}
System.Threading.TimerCallback timercallback = new System.Threading.TimerCallback(tClass.MyCallBackMethod);
timer = new System.Threading.Timer(timercallback,tClass,3000,1000);//period//时期
}
//关闭定时器
private void btnCloseTimer_Click(object sender, EventArgs e)
{
if (timer != null)
{
timer.Dispose();
}
}
}
public class MyThreadClass
{
public void MyCallBackMethod(object obj)
{
DateTime timer = DateTime.Now;
string Info = "现在的时间是:" + timer.ToString();
MessageBox.Show(Info,"信息提示:",MessageBoxButtons.OK);
}
}

//用几个不明白, 请指教!
//timer = new System.Threading.Timer(timercallback,tClass,3000,1000);//period//时期
//3000:是什么意思
//1000:是什么意思

////最后,问一问,这种 定时器,与 直接new 一个 timer 的定时器(最下面介绍那种)有什么不同?

//下面我 继续 介绍我会的 Window 定时器:
大家就说说,两种定时器的异同!!! 有劳各位。
public void mytimerTick(object sender, EventArgs e)
{
rtbShow.Text = DateTime.Now.ToString();
//MessageBox.Show("你好!");
}
public System.Windows.Forms.Timer windowTimer=null;
private void btnWindowTimer_Click(object sender, EventArgs e)
{
if (windowTimer == null)
{
windowTimer = new System.Windows.Forms.Timer();
windowTimer.Interval = 1 * 1000;//1 秒钟
windowTimer.Tick += new EventHandler(this.mytimerTick);
windowTimer.Start();
}
else
{
windowTimer.Dispose();
windowTimer = null;
}
}
...全文
111 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hetengfei_ 2012-01-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 startstartsvip 的回复:]
threading.timer 还有 system.timers.timer 快 准 省资源,用的是线程池资源
3000 是启动时间 1000 是启动后 触发周期
windows.forms.timer/windows.threading.dispatchertimer 是 单线程 易用,安全,可以 update 界面
[/Quote]
谢谢了!
startstartsvip 2012-01-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hetengfei_ 的回复:]

引用 1 楼 startstartsvip 的回复:
Timer 区别呀
你看看 命名空间 不就知道了吗
System.Windows.Forms.Timer UI 的哦,所以能rtbShow.Text = DateTime.Now.ToString();你懂的

请问:
它们在性能上,用什么区别? 那个定时器优先级高?
哪个,要多耗资源?
[/Quote]

threading.timer 还有 system.timers.timer 快 准 省资源,用的是线程池资源
3000 是启动时间 1000 是启动后 触发周期

windows.forms.timer/windows.threading.dispatchertimer 是 单线程 易用,安全,可以 update 界面
hetengfei_ 2012-01-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 startstartsvip 的回复:]
Timer 区别呀
你看看 命名空间 不就知道了吗
System.Windows.Forms.Timer UI 的哦,所以能rtbShow.Text = DateTime.Now.ToString();你懂的
[/Quote]
请问:
它们在性能上,用什么区别? 那个定时器优先级高?
哪个,要多耗资源?
hetengfei_ 2012-01-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zh6335901 的回复:]
为什么每次你发帖都要加◆◆◆◆◆。。
[/Quote]
因為 ◆◆◆◆◆。引人注目,而我的贴是很少人回的!
zh6335901 2012-01-01
  • 打赏
  • 举报
回复
为什么每次你发帖都要加◆◆◆◆◆。。
startstartsvip 2012-01-01
  • 打赏
  • 举报
回复
Timer 区别呀

你看看 命名空间 不就知道了吗

System.Windows.Forms.Timer UI 的哦,所以能rtbShow.Text = DateTime.Now.ToString();你懂的

110,538

社区成员

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

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

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