将定时器与实时同步 [英] Synchronize Timer with real time
问题描述
我正在尝试使用 timer
每 17ms
刷新我的帧.
Timer timer = new Timer(17);timer.Elapsed += ResetFrame;定时器开始();
但不是等待 17ms
然后重复,而是等待帧刷新完成,然后等待 17ms
进行下一次重复.这会导致每 28ms
刷新帧.如何与实时同步?
要拥有一个间隔很短的实时计时器,您可以看看这篇文章:
<块引用>在 Dot Net 中,跟随计时器不是实时的.
System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer
表示如果您想每 100 毫秒运行一次代码,则高于计时器甚至在 110 毫秒或更晚的时间内触发.Windows 不是真的因为这个.Net的时间操作系统也不是实时的.
要在 C# 中创建实时计时器,您必须编写自定义代码可以让 CPU 在正确的时间运行你的代码.
class 程序{static void Main(string[] args){Console.ReadLine();Console.WriteLine(运行");RealTimeTimerTest obj = new RealTimeTimerTest();obj.Run();}}公共类 RealTimeTimerTest{列表<日期时间>lst = new List();System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();公共无效运行(){整数刻度 = 100;int Sleep = Tick - 20;long OldElapsedMilliseconds = 0;sw.开始();while (sw.IsRunning){long ElapsedMilliseconds = sw.ElapsedMilliseconds;long mod = (ElapsedMilliseconds % Tick);if (OldElapsedMilliseconds != ElapsedMilliseconds && (mod == 0 || ElapsedMilliseconds > Tick)){//-----------------在这里做任何你想做的事--------------开始lst.Add(DateTime.Now);//-----------------在这里做任何你想做的事--------------结束//-----------------重启----------------开始OldElapsedMilliseconds = ElapsedMilliseconds;OldElapsedMilliseconds = 0;sw.重置();sw.开始();System.Threading.Thread.Sleep(睡眠);//-----------------重启----------------结束}//------------必须在这里定义一些条件来打破循环-----------开始如果(lst.Count > 500){写();休息;}//------------必须在这里定义一些条件来打破循环-----------结束}}私有无效写入(){System.IO.StreamWriter sw = new System.IO.StreamWriter("d:\\text.txt", true);foreach (DateTime dtStart in lst)sw.WriteLine(dtStart.ToString("HH:mm:ss.ffffff"));sw.关闭();}}
还有:
Precision-Repeat-Action-On-Interval-Async-Method
I am trying to refresh my frame every 17ms
with a timer
.
Timer timer = new Timer(17);
timer.Elapsed += ResetFrame;
timer.Start();
But instead of waiting for 17ms
and then repeating, it waited for the frame refresh to complete and then wait for 17ms
for the next repeat. This causes the frame to be refreshed every 28ms
. How to synchronize it with real time?
To have a real time timer having a very short interval, you can take a look at this article:
In Dot Net, following timers are not real time.
System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer
Means if you want to run your code at every 100 millisecond then above timer fire even around 110 millisecond or later. Windows is not a real time OS because of this .Net is also not a real time.
To create a real time timer in C# you have to write custom code that can hold CPU to run your code at right time.
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
Console.WriteLine("Running");
RealTimeTimerTest obj = new RealTimeTimerTest();
obj.Run();
}
}
public class RealTimeTimerTest
{
List<DateTime> lst = new List<DateTime>();
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
public void Run()
{
int Tick = 100;
int Sleep = Tick - 20;
long OldElapsedMilliseconds = 0;
sw.Start();
while (sw.IsRunning)
{
long ElapsedMilliseconds = sw.ElapsedMilliseconds;
long mod = (ElapsedMilliseconds % Tick);
if (OldElapsedMilliseconds != ElapsedMilliseconds && (mod == 0 || ElapsedMilliseconds > Tick))
{
//-----------------Do here whatever you want to do--------------Start
lst.Add(DateTime.Now);
//-----------------Do here whatever you want to do--------------End
//-----------------Restart----------------Start
OldElapsedMilliseconds = ElapsedMilliseconds;
OldElapsedMilliseconds = 0;
sw.Reset();
sw.Start();
System.Threading.Thread.Sleep(Sleep);
//-----------------Restart----------------End
}
//------------Must define some condition to break the loop here-----------Start
if (lst.Count > 500)
{
Write();
break;
}
//-------------Must define some condition to break the loop here-----------End
}
}
private void Write()
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("d:\\text.txt", true);
foreach (DateTime dtStart in lst)
sw.WriteLine(dtStart.ToString("HH:mm:ss.ffffff")); sw.Close();
}
}
Also that:
Microsecond and Millisecond C# Timer
Precision-Repeat-Action-On-Interval-Async-Method
这篇关于将定时器与实时同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!