mirror of
https://github.com/JuniorDark/RustyHearts-Launcher.git
synced 2026-05-07 05:21:44 -04:00
Add project files.
This commit is contained in:
commit
5d3b4542bf
120 changed files with 36258 additions and 0 deletions
40
RHLauncher/ProgressThrottler.cs
Normal file
40
RHLauncher/ProgressThrottler.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace RHLauncher.RHLauncher
|
||||
{
|
||||
public class ProgressThrottler : IProgress<ProgressReport>
|
||||
{
|
||||
private readonly IProgress<ProgressReport> _progress;
|
||||
private readonly int _intervalMs;
|
||||
private readonly TaskScheduler _scheduler;
|
||||
|
||||
private readonly object _lock = new();
|
||||
private bool _isScheduled = false;
|
||||
private ProgressReport? _latestReport;
|
||||
|
||||
public ProgressThrottler(IProgress<ProgressReport> progress, int intervalMs)
|
||||
{
|
||||
_progress = progress;
|
||||
_intervalMs = intervalMs;
|
||||
_scheduler = TaskScheduler.FromCurrentSynchronizationContext();
|
||||
}
|
||||
|
||||
public void Report(ProgressReport value)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_latestReport = value;
|
||||
if (!_isScheduled)
|
||||
{
|
||||
_isScheduled = true;
|
||||
Task.Delay(_intervalMs, _latestReport.CancellationToken).ContinueWith(_ =>
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_progress.Report(_latestReport);
|
||||
_isScheduled = false;
|
||||
}
|
||||
}, _latestReport.CancellationToken, TaskContinuationOptions.ExecuteSynchronously, _scheduler);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue