Skip to content

Commit

Permalink
Improve RevisionDataGridView responsiveness
Browse files Browse the repository at this point in the history
(cherry picked from commit 4bbe805)
  • Loading branch information
mstv committed Sep 24, 2021
1 parent 8b5e21c commit fb4bcd6
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Threading.Tasks;

namespace GitUI.UserControls.RevisionGrid
{
public sealed partial class RevisionDataGridView
{
/// <summary>
/// Coordinates background update executions. Requested reruns only "queue up" upto one.
/// </summary>
private class BackgroundUpdater
{
private readonly Func<Task> _operation;
private readonly int _cooldownMilliseconds;
private readonly object _sync = new();

private volatile bool _executing;
private volatile bool _rerunRequested;

public BackgroundUpdater(Func<Task> operation, int cooldownMilliseconds)
{
_operation = operation ?? throw new ArgumentNullException(nameof(operation));
_cooldownMilliseconds = cooldownMilliseconds;
}

public void ScheduleExcecution()
{
lock (_sync)
{
if (!_executing)
{
// if not running, start it
_executing = true;
Task.Run(WrappedOperationAsync);
}
else
{
// if currently running make sure it runs again
_rerunRequested = true;
}
}
}

private async Task WrappedOperationAsync()
{
await _operation();

if (_rerunRequested)
{
await Task.Delay(_cooldownMilliseconds);
}

lock (_sync)
{
if (_rerunRequested)
{
Task.Run(WrappedOperationAsync);
_rerunRequested = false;
}
else
{
_executing = false;
}
}
}
}
}
}
Loading

0 comments on commit fb4bcd6

Please sign in to comment.