Skip to content

Commit

Permalink
Improve RevisionDataGridView responsiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
wischi-chr committed Feb 1, 2021
1 parent cbc8c05 commit 4bbe805
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 134 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(WrappedOperation);
}
else
{
// if currently running make sure it runs again
_rerunRequested = true;
}
}
}

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

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

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

0 comments on commit 4bbe805

Please sign in to comment.