弱弱地问一下System.Threading.Timer 暂停方法

随风v5 2009-04-16 09:11:33
System.Threading.Timer 新建一个计时器,已经启动了,现在我想将它暂停几秒后再继续运行,应该怎么做?

不要说用 Thread.Sleep(XXX),这样会导致这个主程序都会暂停..这个是我不想看到的
...全文
505 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericzhangbo1982111 2009-04-20
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;




public class TimerClass : IDisposable

{

public TimerClass()

{

//

// TODO: Add constructor logic here

//

}



private Timer _timer;

private bool blnDisposed = false;



public void Start()

{

_timer = new Timer(new TimerCallback(JobCallBack), null, 1000, 1000);

}



private void JobCallBack(object state)

{

_timer.Change(Timeout.Infinite, Timeout.Infinite);

Debug.WriteLine("Starting...");

Thread.Sleep(10000);

Debug.WriteLine("Complete");

if (!blnDisposed)

_timer.Change(1000, 1000);

else

_timer = null;

}



public void Dispose()

{

if (_timer != null)

{

blnDisposed = true;

while (_timer != null)

Thread.Sleep(5);

}

Debug.WriteLine("Disposed");

}

}
随风v5 2009-04-20
  • 打赏
  • 举报
回复
没有人.在等待..
随风v5 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 csrwgs 的回复:]
int OrientTimerInterval=1000; //假设原来1秒触发一次
bool TimerEnableFlag; //用这个来控制Timer。
TimerTick(Sender,EveentArgs)
{
Timer.Enabled=false;
if(TimerEnableFlag)
{
Timer.Interval=3000; //暂停3秒,直到TimerEnableFlag=false为止
Timer.Enabled=true;
return;
}
Timer.Interval = OrientTimerInterval;

[/Quote]

你这个计数器不是我这个哦,
我的是Threading.timer下面的哦.

private System.Threading.Timer timerClose;


// Create a timer thread and start it
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), this, 5000, 0);

龙宜坡 2009-04-16
  • 打赏
  • 举报
回复
C#按钮事件中有循环,用另一个按钮控制停止,暂停,继续程序执行


首先在窗体上有
lable1,//运行显示
button1,//开始
button2,//暂停和继续
button3//停止
窗体上还放Timer控件timer1

代码实现如下,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testMain
{
public partial class Form1 : Form
{
public enum RunState
{
running,
pause,
stop
}

private int i = 0;

RunState state = RunState.stop;

public Form1()
{
InitializeComponent();
}

private void RunProc(object sender,EventArgs e)//执行的部分
{
timer1.Enabled = false;
if (state == RunState.running)
{
label1.Refresh();
label1.Text = "";
label1.Text = i.ToString();
label1.Refresh();
i++;
timer1.Enabled = true;
}
}

private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "开始")
{
this.button1.Enabled = false;
this.button2.Enabled = true;
this.button3.Enabled = true;

timer1.Tick += new EventHandler(RunProc);
timer1.Enabled = true;

this.state = RunState.running;
}
}

private void button2_Click(object sender, EventArgs e)
{
if (this.button2.Text == "暂停")
{
timer1.Enabled = false;
state = RunState.pause;

this.button2.Text = "继续";
}
else
{
timer1.Enabled = true;
this.state = RunState.running;
this.button2.Text = "暂停";
this.button2.Enabled = true;
//timer1.Start();
}
}

private void button3_Click(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.i = 0;
this.label1.Text = "0";
this.state = RunState.stop;

this.button1.Text = "开始";
this.button1.Enabled = true;
this.button2.Text = "暂停";
this.button2.Enabled = false;
this.button3.Enabled=false;
}

private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text = "0";
this.button2.Enabled = false;
this.button3.Enabled = false;
}
}
}



以前搞过,有点勉强!
csrwgs 2009-04-16
  • 打赏
  • 举报
回复
int OrientTimerInterval=1000; //假设原来1秒触发一次
bool TimerEnableFlag; //用这个来控制Timer。
TimerTick(Sender,EveentArgs)
{
Timer.Enabled=false;
if(TimerEnableFlag)
{
Timer.Interval=3000; //暂停3秒,直到TimerEnableFlag=false为止
Timer.Enabled=true;
return;
}
Timer.Interval = OrientTimerInterval;
//your work.

Timer.Enabled=true;
}

110,502

社区成员

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

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

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