Skip to content

Commit

Permalink
Core: Cancel previous operation to update Task
Browse files Browse the repository at this point in the history
  • Loading branch information
goodtrailer committed Sep 5, 2023
1 parent ec45bfd commit 2cd82d9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
15 changes: 14 additions & 1 deletion DailyDesktop.Core/DailyDesktopCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,23 @@ private DailyDesktopCore(PathConfiguration pathConfig, string taskName)
};
}

private CancellationTokenSource? previousTokenSource;

private async Task onTaskConfigUpdateAsync(object? sender, EventArgs? args, CancellationToken cancellationToken)
{
if (IsAutoCreatingTask)
await Task.Run(CreateTask, cancellationToken);
{
try
{
previousTokenSource?.Cancel();
previousTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
await Task.Run(CreateTask, previousTokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Win32 Task update operation cancelled.");
}
}
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions DailyDesktop.Core/Providers/ProviderStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public async Task ScanAsync(string directory, CancellationToken cancellationToke

foreach (var path in Directory.GetFiles(directory, PROVIDERS_SEARCH_PATTERN, SearchOption.AllDirectories))
{
if (cancellationToken.IsCancellationRequested)
throw new OperationCanceledException();
cancellationToken.ThrowIfCancellationRequested();

try
{
Expand Down
3 changes: 3 additions & 0 deletions DailyDesktop.Core/Util/AsyncEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public static async Task InvokeAsync(this AsyncEventHandler? handler, object sen
return;

foreach (var d in delegates)
{
cancellationToken.ThrowIfCancellationRequested();
await d.Invoke(sender, args, cancellationToken);
}
}

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions DailyDesktop.Desktop/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions DailyDesktop.Desktop/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public partial class MainForm : Form
private IPublicTaskConfiguration taskConfig => core.TaskConfig;

private TaskState previousState;

private bool isMovingBlurStrengthTrackBar;

public static async Task<MainForm> CreateFormAsync(CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -103,19 +105,22 @@ private async void MainForm_Load(object? sender, EventArgs e)
{
await repopulateProviderComboBox();

optionsEnabledCheckBox.Checked = taskConfig.IsEnabled;
Invoke(() =>
{
optionsEnabledCheckBox.Checked = taskConfig.IsEnabled;

optionsUpdateTimePicker.Value = taskConfig.UpdateTime;
optionsUpdateTimePicker.Enabled = optionsEnabledCheckBox.Checked;
optionsUpdateTimePicker.Value = taskConfig.UpdateTime;
optionsUpdateTimePicker.Enabled = optionsEnabledCheckBox.Checked;

optionsResizeCheckBox.Checked = taskConfig.DoResize;
optionsResizeCheckBox.Checked = taskConfig.DoResize;

optionsBlurredFitCheckBox.Checked = taskConfig.DoBlurredFit;
optionsBlurStrengthTrackBar.Value = taskConfig.BlurStrength;
optionsBlurStrengthTrackBar.Enabled = optionsBlurredFitCheckBox.Checked;
updateBlurStrengthToolTip();
optionsBlurredFitCheckBox.Checked = taskConfig.DoBlurredFit;
optionsBlurStrengthTrackBar.Value = taskConfig.BlurStrength;
optionsBlurStrengthTrackBar.Enabled = optionsBlurredFitCheckBox.Checked;
updateBlurStrengthToolTip();

stateBackgroundWorker.RunWorkerAsync();
stateBackgroundWorker.RunWorkerAsync();
});
}

private void MainForm_FormClosing(object? sender, EventArgs e)
Expand Down Expand Up @@ -192,18 +197,18 @@ private async Task repopulateProviderComboBox()
private async void optionsEnabledCheckBox_CheckedChanged(object? sender, EventArgs e)
{
await taskConfig.SetIsEnabledAsync(optionsEnabledCheckBox.Checked, AsyncUtils.TimedCancel());
optionsUpdateTimePicker.Enabled = optionsEnabledCheckBox.Checked;
Invoke(() => optionsUpdateTimePicker.Enabled = optionsEnabledCheckBox.Checked);
}

private void optionsUpdateWallpaperButton_Click(object? sender, EventArgs e) => core.UpdateWallpaper();

private void optionsProvidersDirectoryButton_Click(object? sender, EventArgs e) => openUri(core.PathConfig.ProvidersDir);

private async void optionsBlurStrengthTrackBar_Scroll(object? sender, EventArgs e)
{
await taskConfig.SetBlurStrengthAsync(optionsBlurStrengthTrackBar.Value, AsyncUtils.TimedCancel());
updateBlurStrengthToolTip();
}
private void optionsBlurStrengthTrackBar_Scroll(object? sender, EventArgs e) => updateBlurStrengthToolTip();

private async void optionsBlurStrengthTrackBar_MouseUp(object? sender, MouseEventArgs e) => await taskConfig.SetBlurStrengthAsync(optionsBlurStrengthTrackBar.Value, AsyncUtils.TimedCancel());

private async void optionsBlurStrengthTrackBar_KeyUp(object? sender, KeyEventArgs e) => await taskConfig.SetBlurStrengthAsync(optionsBlurStrengthTrackBar.Value, AsyncUtils.TimedCancel());

private void updateBlurStrengthToolTip()
{
Expand All @@ -214,7 +219,7 @@ private void updateBlurStrengthToolTip()
private async void optionsBlurredFitCheckBox_CheckedChanged(object? sender, EventArgs e)
{
await taskConfig.SetDoBlurredFitAsync(optionsBlurredFitCheckBox.Checked, AsyncUtils.TimedCancel());
optionsBlurStrengthTrackBar.Enabled = optionsBlurredFitCheckBox.Checked;
Invoke(() => optionsBlurStrengthTrackBar.Enabled = optionsBlurredFitCheckBox.Checked);
}

private async void optionsResizeCheckBox_CheckedChanged(object? sender, EventArgs e)
Expand Down

0 comments on commit 2cd82d9

Please sign in to comment.