Skip to content

Commit

Permalink
Prevent Too Early Autosaves
Browse files Browse the repository at this point in the history
  • Loading branch information
DelnarErsike committed May 14, 2024
1 parent 123c940 commit a5f1c64
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Chummer/Forms/Character Forms/CharacterShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,25 @@ CancellationTokenRegistration objCancellationRegistration
}
_tmrCharacterUpdateRequestTimer.Elapsed += CharacterUpdateRequestTimerOnElapsed;
_tmrCharacterUpdateRequestTimer.Start();
_tmrAutosaveRequestTimer.Elapsed += AutosaveRequestTimerOnElapsed;
AutosaveStopwatch.Start();
_tmrAutosaveRequestTimer.Start();
}

private async void AutosaveRequestTimerOnElapsed(object sender, ElapsedEventArgs e)
{
try
{
GenericToken.ThrowIfCancellationRequested();
if (IsDirty && AutosaveStopwatch?.Elapsed.Minutes >= 5)
{
await AutoSaveCharacter(GenericToken).ConfigureAwait(false);
}
}
catch (OperationCanceledException)
{
// swallow this
}
}

[Obsolete("This constructor is for use by form designers only.", true)]
Expand Down Expand Up @@ -303,6 +321,8 @@ public override string ToString()

protected Stopwatch AutosaveStopwatch => _stpAutosaveStopwatch;

private System.Timers.Timer _tmrAutosaveRequestTimer = new System.Timers.Timer(1000 * 300);

private DebuggableSemaphoreSlim _objAutosaveSemaphore = new DebuggableSemaphoreSlim();

/// <summary>
Expand All @@ -325,6 +345,7 @@ protected async Task AutoSaveCharacter(CancellationToken token = default)

try
{
_tmrAutosaveRequestTimer?.Stop();
CursorWait objCursorWait = await CursorWait.NewAsync(this, true, token).ConfigureAwait(false);
try
{
Expand Down Expand Up @@ -381,6 +402,7 @@ await LanguageManager
}
finally
{
_tmrAutosaveRequestTimer?.Start();
_objAutosaveSemaphore?.Release();
}
}
Expand Down Expand Up @@ -9930,10 +9952,6 @@ public async Task SetDirty(bool blnValue, CancellationToken token = default)
int intNewValue = blnValue.ToInt32();
if (Interlocked.Exchange(ref _intIsDirty, intNewValue) != intNewValue)
await UpdateWindowTitleAsync(true, token).ConfigureAwait(false);
if (blnValue && AutosaveStopwatch?.Elapsed.Minutes >= 5)
{
await AutoSaveCharacter(token).ConfigureAwait(false);
}
}

/// <summary>
Expand Down Expand Up @@ -10092,7 +10110,7 @@ public async void MakeDirty(object sender, EventArgs e)
}
}

private System.Timers.Timer _tmrCharacterUpdateRequestTimer = new System.Timers.Timer(500);
private System.Timers.Timer _tmrCharacterUpdateRequestTimer = new System.Timers.Timer(500) { AutoReset = true };

private async void CharacterUpdateRequestTimerOnElapsed(object sender, ElapsedEventArgs e)
{
Expand Down Expand Up @@ -10641,6 +10659,7 @@ protected override void Dispose(bool disposing)
}
}
dlgSaveFile?.Dispose();
Interlocked.Exchange(ref _tmrAutosaveRequestTimer, null)?.Dispose();
Interlocked.Exchange(ref _tmrCharacterUpdateRequestTimer, null)?.Dispose();
Interlocked.Exchange(ref _objCharacterUpdateStartingSemaphore, null)?.Dispose();
Interlocked.Exchange(ref _objAutosaveSemaphore, null)?.Dispose();
Expand Down

0 comments on commit a5f1c64

Please sign in to comment.