using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace PTest
{
class Program
{
static void Main(string[] args)
{
Xple x = new Xple();
x.GGO();
}
}
class Xple
{
System.Windows.Forms.Timer ntime;
public void GGO()
{
ntime = new System.Windows.Forms.Timer();
ntime.Interval = 777;
ntime.Tick += delegate
{
Console.WriteLine("进入计时器");
};
Pple a = new Pple();
a.ldasd += delegate
{
Console.WriteLine("开始");
ntime.Start();
};
a.GoWalk(1000);
Console.Read();
}
}
class Pple
{
public delegate void MMethod();
private System.Timers.Timer walk=new System.Timers.Timer();
private int steps=0;
public event MMethod ldasd;
public void GoWalk(int t)
{
walk.Interval = t;
walk.Elapsed += Walk_Elapsed;
walk.Start();
}
private void Walk_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("第"+(++steps).ToString()+"步");
if (steps == 5)
{
ldasd();
}
}
}
}
为什么能看到调用了Start() 方法,却不进入它的Tick事件 ?Console.WriteLine("进入计时器") 这句话没有执行。
System.Windows.Forms.Timer不适用于非winform界面的场景(需要ui线程),你可以换用System.Timers.Timer或者System.Threading.Timer
有冲突的不,