改善 C#(.Net) 应用程序中我的周期的延迟
Improve the Latency of my cycle in C#(.Net) application
我有一个关于获得非常高延迟的一般性问题。我正在为具有 Windows Embedded Pro 7 的目标设备编码。所以我假设我可以获得实时性能(根据我所阅读的内容)。我正在使用“System.Timers”来设置时间 cycle.Below 是
中的示例
public void updateCycle50ms( )
{
Stopwatch t = Stopwatch.StartNew();
System.TimeSpan timer50ms = System.TimeSpan.FromMilliseconds(50);
while (1 == 1)
{
// Sending Message
CANSEND(ref msg); // This function sends Message over CAN network.
while (t.Elapsed < timer50ms)
{
// do nothing
}
}
}
我尝试做的是每 50 毫秒 发送一条消息,但周期从 29 毫秒到 90 毫秒(我可以在接收端看到它)。你们能告诉我为什么我无法实现我的目标吗?我是否需要使用另一个 .Net class 或者有特殊的 classes 可以在 Windows Embedded 中使用以获得实时性能(或更接近它)。
尝试使用System.Timers.Timer class:
private System.Timers.Timer timer;
public void updateCycle50ms( )
{
// Create a timer with a 50ms interval.
timer= new System.Timers.Timer(50);
// Hook up the Elapsed event for the timer.
timer.Elapsed += (s, e) =>
{
// Sending Message
CANSEND(ref msg);
};
// Have the timer fire repeated events (true is the default)
timer.AutoReset = true;
// Start the timer
timer.Enabled = true;
// If the timer is declared in a long-running method, use KeepAlive to prevent garbage collection
// from occurring before the method ends.
// GC.KeepAlive(timer)
}
我有一个关于获得非常高延迟的一般性问题。我正在为具有 Windows Embedded Pro 7 的目标设备编码。所以我假设我可以获得实时性能(根据我所阅读的内容)。我正在使用“System.Timers”来设置时间 cycle.Below 是
中的示例 public void updateCycle50ms( )
{
Stopwatch t = Stopwatch.StartNew();
System.TimeSpan timer50ms = System.TimeSpan.FromMilliseconds(50);
while (1 == 1)
{
// Sending Message
CANSEND(ref msg); // This function sends Message over CAN network.
while (t.Elapsed < timer50ms)
{
// do nothing
}
}
}
我尝试做的是每 50 毫秒 发送一条消息,但周期从 29 毫秒到 90 毫秒(我可以在接收端看到它)。你们能告诉我为什么我无法实现我的目标吗?我是否需要使用另一个 .Net class 或者有特殊的 classes 可以在 Windows Embedded 中使用以获得实时性能(或更接近它)。
尝试使用System.Timers.Timer class:
private System.Timers.Timer timer;
public void updateCycle50ms( )
{
// Create a timer with a 50ms interval.
timer= new System.Timers.Timer(50);
// Hook up the Elapsed event for the timer.
timer.Elapsed += (s, e) =>
{
// Sending Message
CANSEND(ref msg);
};
// Have the timer fire repeated events (true is the default)
timer.AutoReset = true;
// Start the timer
timer.Enabled = true;
// If the timer is declared in a long-running method, use KeepAlive to prevent garbage collection
// from occurring before the method ends.
// GC.KeepAlive(timer)
}