110,538
社区成员
发帖
与我相关
我的任务
分享
//
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);
}
}
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;
}
}