永久会员
- 威望
- 3418
- 贡献
- 5111
- 热心值
- 0
- 金币
- 3125
- 注册时间
- 2019-8-13
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFFormUse
{
public partial class FrmTimer : Form
{
public FrmTimer()
{
InitializeComponent();
}
//每隔指定的时间间隔就会触发
private void timer1_Tick(object sender, EventArgs e)
{
lblDateTime.Text = DateTime.Now.ToString();
}
System.Threading.Timer timer4;
private void FrmTimer_Load(object sender, EventArgs e)
{
//UI
lblDateTime.Text = DateTime.Now.ToString();
timer1.Enabled = true;
//服务
System.Timers.Timer timer3 = new System.Timers.Timer();
timer3.Interval = 1000;
//timer3.AutoReset = false;//只会引发一次,就停止了
timer3.Elapsed += Timer3_Elapsed;
timer3.Start();
//多线程
//period 时间间隔 0或-1 只会执行一 次,然后就停止
//Change 可以让计时器重新启动
//停止 period 0 -1 timer4.Dispose()
int count2 = 0;
timer4 = new System.Threading.Timer(new System.Threading.TimerCallback(o => {
count2 += 2;
Action<int> act = ShowCount;
this.Invoke(act, count2);
}), null, 1000,1000);
//timer4.Change(2000, 2000);//改变延迟启动时间和时间间隔
}
private void ShowCount(int cout)
{
txtCount.Text = cout.ToString();
if (cout > 50)
timer4.Dispose();
}
int count = 0;
private void Timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//lblDt2.Text= DateTime.Now.ToString();
count += 1;
Action act = UpdateTime;
this.Invoke(act);
if(count >10)
{
System.Timers.Timer timer = (System.Timers.Timer)sender;
timer.Stop();
}
}
private void UpdateTime()
{
lblDt2.Text = DateTime.Now.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFFormUse
{
public partial class FrmTimer : Form
{
public FrmTimer()
{
InitializeComponent();
}
//每隔指定的时间间隔就会触发
private void timer1_Tick(object sender, EventArgs e)
{
lblDateTime.Text = DateTime.Now.ToString();
}
System.Threading.Timer timer4;
private void FrmTimer_Load(object sender, EventArgs e)
{
//UI
lblDateTime.Text = DateTime.Now.ToString();
timer1.Enabled = true;
//服务
System.Timers.Timer timer3 = new System.Timers.Timer();
timer3.Interval = 1000;
//timer3.AutoReset = false;//只会引发一次,就停止了
timer3.Elapsed += Timer3_Elapsed;
timer3.Start();
//多线程
//period 时间间隔 0或-1 只会执行一 次,然后就停止
//Change 可以让计时器重新启动
//停止 period 0 -1 timer4.Dispose()
int count2 = 0;
timer4 = new System.Threading.Timer(new System.Threading.TimerCallback(o => {
count2 += 2;
Action<int> act = ShowCount;
this.Invoke(act, count2);
}), null, 1000,1000);
//timer4.Change(2000, 2000);//改变延迟启动时间和时间间隔
}
private void ShowCount(int cout)
{
txtCount.Text = cout.ToString();
if (cout > 50)
timer4.Dispose();
}
int count = 0;
private void Timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//lblDt2.Text= DateTime.Now.ToString();
count += 1;
Action act = UpdateTime;
this.Invoke(act);
if(count >10)
{
System.Timers.Timer timer = (System.Timers.Timer)sender;
timer.Stop();
}
}
private void UpdateTime()
{
lblDt2.Text = DateTime.Now.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WFFormUse
{
public partial class FrmTimer : Form
{
public FrmTimer()
{
InitializeComponent();
}
//每隔指定的时间间隔就会触发
private void timer1_Tick(object sender, EventArgs e)
{
lblDateTime.Text = DateTime.Now.ToString();
}
System.Threading.Timer timer4;
private void FrmTimer_Load(object sender, EventArgs e)
{
//UI
lblDateTime.Text = DateTime.Now.ToString();
timer1.Enabled = true;
//服务
System.Timers.Timer timer3 = new System.Timers.Timer();
timer3.Interval = 1000;
//timer3.AutoReset = false;//只会引发一次,就停止了
timer3.Elapsed += Timer3_Elapsed;
timer3.Start();
//多线程
//period 时间间隔 0或-1 只会执行一 次,然后就停止
//Change 可以让计时器重新启动
//停止 period 0 -1 timer4.Dispose()
int count2 = 0;
timer4 = new System.Threading.Timer(new System.Threading.TimerCallback(o => {
count2 += 2;
Action<int> act = ShowCount;
this.Invoke(act, count2);
}), null, 1000,1000);
//timer4.Change(2000, 2000);//改变延迟启动时间和时间间隔
}
private void ShowCount(int cout)
{
txtCount.Text = cout.ToString();
if (cout > 50)
timer4.Dispose();
}
int count = 0;
private void Timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//lblDt2.Text= DateTime.Now.ToString();
count += 1;
Action act = UpdateTime;
this.Invoke(act);
if(count >10)
{
System.Timers.Timer timer = (System.Timers.Timer)sender;
timer.Stop();
}
}
private void UpdateTime()
{
lblDt2.Text = DateTime.Now.ToString();
}
}
}
|
|