nanoFramework ESP32 Thread.Sleep 奇怪的行为
nanoFramework ESP32 Thread.Sleep weird behaviour
我在 ESP32 上有以下程序 运行 作为测试:
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Hardware.Esp32;
namespace NFApp2
{
public class Program
{
private static long Counter;
private static long Ticks;
public static void Main()
{
Ticks = 5000;
Thread CountThread = new Thread(new ThreadStart(CountFunction));
CountThread.Start();
while (true)
{
Thread.Sleep(1000);
Debug.WriteLine(string.Format("Ticks = {0}, Count = {1}", Ticks, Counter));
Counter = 0;
Ticks += 100;
}
}
private static void CountFunction()
{
while (true)
{
Thread.Sleep(new TimeSpan(Ticks));
Counter++;
}
}
}
}
基本上是启动一个线程休眠一段时间 (Ticks),然后将计数器加 1。
每秒打印并重置计数,线程的睡眠时间增加 100 个刻度。
当 运行 时,我得到以下输出:
Ticks = 5000, Count = 3445
Ticks = 5100, Count = 3435
Ticks = 5200, Count = 3405
Ticks = 5300, Count = 3405
Ticks = 5400, Count = 3403
Ticks = 5500, Count = 3406
Ticks = 5600, Count = 3405
Ticks = 5700, Count = 3404
Ticks = 5800, Count = 3403
Ticks = 5900, Count = 3403
Ticks = 6000, Count = 3403
Ticks = 6100, Count = 3406
Ticks = 6200, Count = 3402
Ticks = 6300, Count = 3404
Ticks = 6400, Count = 3403
Ticks = 6500, Count = 3403
Ticks = 6600, Count = 3405
Ticks = 6700, Count = 3406
Ticks = 6800, Count = 3403
Ticks = 6900, Count = 3401
Ticks = 7000, Count = 3403
Ticks = 7100, Count = 3403
Ticks = 7200, Count = 3403
Ticks = 7300, Count = 3407
Ticks = 7400, Count = 3403
Ticks = 7500, Count = 3403
Ticks = 7600, Count = 3404
Ticks = 7700, Count = 3402
Ticks = 7800, Count = 3404
Ticks = 7900, Count = 3406
Ticks = 8000, Count = 3402
Ticks = 8100, Count = 3402
Ticks = 8200, Count = 3403
Ticks = 8300, Count = 3403
Ticks = 8400, Count = 3404
Ticks = 8500, Count = 3407
Ticks = 8600, Count = 3403
Ticks = 8700, Count = 3403
Ticks = 8800, Count = 3403
Ticks = 8900, Count = 3403
Ticks = 9000, Count = 3403
Ticks = 9100, Count = 3407
Ticks = 9200, Count = 3502
Ticks = 9300, Count = 3403
Ticks = 9400, Count = 3403
Ticks = 9500, Count = 3402
Ticks = 9600, Count = 3404
Ticks = 9700, Count = 3407
Ticks = 9800, Count = 3403
Ticks = 9900, Count = 3403
Ticks = 10000, Count = 100
Ticks = 10100, Count = 100
Ticks = 10200, Count = 100
Ticks = 10300, Count = 101
Ticks = 10400, Count = 100
Ticks = 10500, Count = 100
Ticks = 10600, Count = 99
Ticks = 10700, Count = 101
Ticks = 10800, Count = 99
Ticks = 10900, Count = 100
Ticks = 11000, Count = 100
Ticks = 11100, Count = 100
Ticks = 11200, Count = 100
Ticks = 11300, Count = 100
Ticks = 11400, Count = 100
Ticks = 11500, Count = 100
Ticks = 11600, Count = 100
Ticks = 11700, Count = 100
Ticks = 11800, Count = 100
Ticks = 11900, Count = 100
Ticks = 12000, Count = 100
Ticks = 12100, Count = 100
Ticks = 12200, Count = 100
Ticks = 12300, Count = 100
Ticks = 12400, Count = 101
Ticks = 12500, Count = 99
Ticks = 12600, Count = 100
我知道时间永远不会准确,但我希望计数会慢慢下降,因为线程函数需要更长的时间来执行.....但它不会,它保持在 3404 左右。
过了一会儿,当睡眠时间达到 10000 时,发生了一件更奇怪的事情,计数突然下降到 100 左右并保持在那里。
也许我做错了什么或者我的期望是错误的。
有些帮助会很棒!
干杯,
罗伯特.
我认为你的错误是构造函数 new TimeSpan(int)
不需要毫秒,而是 high-resolution 刻度值的倍数,即 100ns。由于 Thread.Sleep()
的最小分辨率为 1 毫秒,因此小于 1 毫秒的时间跨度根本不会等待。因此,只要 Ticks
小于 10.000,您的循环就会尽可能快地运行(显然大约每秒 3400 次)。一旦 Ticks 大于 10.000(对应于 1 毫秒的等待时间),情况就会发生变化:现在循环有规律地等待,每秒大约进行 100 次迭代。现在这很可能是因为 Thread.Sleep()
的最短等待时间为 10 毫秒(该值因硬件而异)。
我在 ESP32 上有以下程序 运行 作为测试:
using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.Hardware.Esp32;
namespace NFApp2
{
public class Program
{
private static long Counter;
private static long Ticks;
public static void Main()
{
Ticks = 5000;
Thread CountThread = new Thread(new ThreadStart(CountFunction));
CountThread.Start();
while (true)
{
Thread.Sleep(1000);
Debug.WriteLine(string.Format("Ticks = {0}, Count = {1}", Ticks, Counter));
Counter = 0;
Ticks += 100;
}
}
private static void CountFunction()
{
while (true)
{
Thread.Sleep(new TimeSpan(Ticks));
Counter++;
}
}
}
}
基本上是启动一个线程休眠一段时间 (Ticks),然后将计数器加 1。 每秒打印并重置计数,线程的睡眠时间增加 100 个刻度。
当 运行 时,我得到以下输出:
Ticks = 5000, Count = 3445
Ticks = 5100, Count = 3435
Ticks = 5200, Count = 3405
Ticks = 5300, Count = 3405
Ticks = 5400, Count = 3403
Ticks = 5500, Count = 3406
Ticks = 5600, Count = 3405
Ticks = 5700, Count = 3404
Ticks = 5800, Count = 3403
Ticks = 5900, Count = 3403
Ticks = 6000, Count = 3403
Ticks = 6100, Count = 3406
Ticks = 6200, Count = 3402
Ticks = 6300, Count = 3404
Ticks = 6400, Count = 3403
Ticks = 6500, Count = 3403
Ticks = 6600, Count = 3405
Ticks = 6700, Count = 3406
Ticks = 6800, Count = 3403
Ticks = 6900, Count = 3401
Ticks = 7000, Count = 3403
Ticks = 7100, Count = 3403
Ticks = 7200, Count = 3403
Ticks = 7300, Count = 3407
Ticks = 7400, Count = 3403
Ticks = 7500, Count = 3403
Ticks = 7600, Count = 3404
Ticks = 7700, Count = 3402
Ticks = 7800, Count = 3404
Ticks = 7900, Count = 3406
Ticks = 8000, Count = 3402
Ticks = 8100, Count = 3402
Ticks = 8200, Count = 3403
Ticks = 8300, Count = 3403
Ticks = 8400, Count = 3404
Ticks = 8500, Count = 3407
Ticks = 8600, Count = 3403
Ticks = 8700, Count = 3403
Ticks = 8800, Count = 3403
Ticks = 8900, Count = 3403
Ticks = 9000, Count = 3403
Ticks = 9100, Count = 3407
Ticks = 9200, Count = 3502
Ticks = 9300, Count = 3403
Ticks = 9400, Count = 3403
Ticks = 9500, Count = 3402
Ticks = 9600, Count = 3404
Ticks = 9700, Count = 3407
Ticks = 9800, Count = 3403
Ticks = 9900, Count = 3403
Ticks = 10000, Count = 100
Ticks = 10100, Count = 100
Ticks = 10200, Count = 100
Ticks = 10300, Count = 101
Ticks = 10400, Count = 100
Ticks = 10500, Count = 100
Ticks = 10600, Count = 99
Ticks = 10700, Count = 101
Ticks = 10800, Count = 99
Ticks = 10900, Count = 100
Ticks = 11000, Count = 100
Ticks = 11100, Count = 100
Ticks = 11200, Count = 100
Ticks = 11300, Count = 100
Ticks = 11400, Count = 100
Ticks = 11500, Count = 100
Ticks = 11600, Count = 100
Ticks = 11700, Count = 100
Ticks = 11800, Count = 100
Ticks = 11900, Count = 100
Ticks = 12000, Count = 100
Ticks = 12100, Count = 100
Ticks = 12200, Count = 100
Ticks = 12300, Count = 100
Ticks = 12400, Count = 101
Ticks = 12500, Count = 99
Ticks = 12600, Count = 100
我知道时间永远不会准确,但我希望计数会慢慢下降,因为线程函数需要更长的时间来执行.....但它不会,它保持在 3404 左右。 过了一会儿,当睡眠时间达到 10000 时,发生了一件更奇怪的事情,计数突然下降到 100 左右并保持在那里。
也许我做错了什么或者我的期望是错误的。
有些帮助会很棒!
干杯, 罗伯特.
我认为你的错误是构造函数 new TimeSpan(int)
不需要毫秒,而是 high-resolution 刻度值的倍数,即 100ns。由于 Thread.Sleep()
的最小分辨率为 1 毫秒,因此小于 1 毫秒的时间跨度根本不会等待。因此,只要 Ticks
小于 10.000,您的循环就会尽可能快地运行(显然大约每秒 3400 次)。一旦 Ticks 大于 10.000(对应于 1 毫秒的等待时间),情况就会发生变化:现在循环有规律地等待,每秒大约进行 100 次迭代。现在这很可能是因为 Thread.Sleep()
的最短等待时间为 10 毫秒(该值因硬件而异)。