/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Engine/ThresholdWait.cs
56 строк
1 KB
EugenyCh7
ThresholdWait fix
25 авг 2025, 20:32
25 авг 2025, 20:32
54eaad8
Код
Авторство
О чём код?
using System; using System.Threading; using System.Threading.Tasks; namespace Puppet2D.Engine { public class ThresholdWait { private double accumulator; public int Threshold { get => Interlocked.CompareExchange(ref field, 0, 0); set => Interlocked.Exchange(ref field, value); } public ThresholdWait() { Threshold = 0; } public ThresholdWait(int threshold) { Threshold = threshold; } public void Reset() { Interlocked.Exchange(ref accumulator, 0.0); } public void Wait(double milliseconds, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); double copy = Interlocked.CompareExchange(ref accumulator, 0, 0); copy += milliseconds; int intAccumulator = (int)copy; if (intAccumulator < Threshold) { Interlocked.Exchange(ref accumulator, copy); return; } else { copy -= intAccumulator; Interlocked.Exchange(ref accumulator, copy); Task.Delay(intAccumulator).Wait(cancellationToken); } } public void Wait(TimeSpan time, CancellationToken cancellationToken = default) { Wait(time.TotalMilliseconds, cancellationToken); } } }