gpt4 book ai didi

c# - 在我的计划循环中重新分配 DateTime 对象的值时,DateTime 多线程 UDP 发送方具有非常不同的时间结果

转载 作者:行者123 更新时间:2023-12-01 20:25:47 28 4
gpt4 key购买 nike

总结: block 之间唯一的代码区别是 DateTime 对象的赋值和重新赋值

我有一个应用程序,可以按照非常严格的“时间表”发送 UDP 数据包。我看到一些行为让我认为我对计时器、线程或其他一些概念有很大的误解 - 我试图使用基于 SendingSchedule 列表的 SendAllMessages() 方法发送数据包。我的发送频率是 12.5Hz,或者每次消息突发之间的延迟不到 0.1 秒。但是,如果我在此方法的循环中重新分配 DateTime 对象,我会看到很多不同的值。请注意,此方法在 4 个不同的任务中运行,即 4 个不同的线程。我的代码的第一个示例给出了我想要和期望的行为:几乎同时发送 4 个数据包,然后 0.08 秒后我看到另一个 4 条消息的突发。代码的第二个示例以慢得多的速度显示消息,但我不明白为什么。我认为两者的行为相同。在我的第二个示例中,我的“时间”对象是否在线程之间以某种方式共享,或者是否发生了其他情况?

工作代码(突发 4 条消息,然后等待 0.08 秒,然后再突发):

private static void SendAllMessages(List<DataMessageFormat> dataMessageList, UDPSender udpSender, byte[] first4bytes, int messageSize, bool loopContinuously = false)
{
// pass inetMessageList to DataMessageEncoder
MessageDataEncoder dataMessageEncoder = new MessageDataEncoder();

List<byte[]> byteArrayListDataMessage = dataMessageEncoder.ConvertFromFormatToByteArray(dataMessageList, first4bytes, messageSize, switchDefaultEndian);

Console.WriteLine("Sending " + first4bytes + " UDP Messages on Thread" + Thread.CurrentThread.ManagedThreadId);
do
{
DateTime start = DateTime.Now;
for (int i = 0; i < byteArrayListDataMessage.Count; i++) // all message lists must have the same count for this to work
{
DateTime time = start.AddSeconds(dataMessageEncoder.SendingSchedule[i]);
Send:
if (DateTime.Now > time)
{
udpSender.SendUDPOnce(byteArrayListDataMessage[i]);
}
else
{
System.Threading.Thread.Sleep(1);
goto Send;
}
}
} while (loopContinuously);
}

下面的代码在突发 4 条消息之间等待很长时间,几乎就像线程都在等待同一个 DateTimeObject:

   private static void SendAllMessages(List<DataMessageFormat> dataMessageList, UDPSender udpSender, byte[] first4bytes, int messageSize, bool loopContinuously = false)
{
// pass inetMessageList to DataMessageEncoder
MessageDataEncoder dataMessageEncoder = new MessageDataEncoder();

List<byte[]> byteArrayListDataMessage = dataMessageEncoder.ConvertFromFormatToByteArray(dataMessageList, first4bytes, messageSize, switchDefaultEndian);

Console.WriteLine("Sending " + first4bytes + " UDP Messages on Thread" + Thread.CurrentThread.ManagedThreadId);
do
{
DateTime time = DateTime.Now;
for (int i = 0; i < byteArrayListDataMessage.Count; i++) // all message lists must have the same count for this to work
{
time = time.AddSeconds(dataMessageEncoder.SendingSchedule[i]);
Send:
if (DateTime.Now > time)
{
udpSender.SendUDPOnce(byteArrayListDataMessage[i]);
}
else
{
System.Threading.Thread.Sleep(1);
goto Send;
}
}
} while (loopContinuously);
}

下面是我的正确代码生成的wireshark屏幕截图 Good timing aka first block of code

下面是我的错误代码导致的wireshark屏幕截图

Bad slow timing aka second block of code

只是为了让您了解这个应用程序正在做什么: Console App simple 1000ft view

最佳答案

两个代码有不同的逻辑:

第一:您要比较的时间值是初始时间加上时间表。一旦达到所需的时间,if 测试可能始终为 true,并且您发送整个 byteArrayListDataMessage.Count 元素。

第二:for 循环的每一步都会创建移动目标,因此只发送一个元素。

关于c# - 在我的计划循环中重新分配 DateTime 对象的值时,DateTime 多线程 UDP 发送方具有非常不同的时间结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com