forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve RevisionDataGridView responsiveness
- Loading branch information
1 parent
cbc8c05
commit 4bbe805
Showing
2 changed files
with
148 additions
and
134 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
GitUI/UserControls/RevisionGrid/RevisionDataGridView.BackgroundUpdater.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.