Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 58e2393

Browse files
fix: should use new CancellationTokenSource for executing command each time (#1012)
Signed-off-by: leo <longshuang@msn.cn>
1 parent d0a32eb commit 58e2393

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

‎src/ViewModels/Clone.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
using Avalonia.Threading;
@@ -101,10 +102,11 @@ public override Task<bool> Sure()
101102
{
102103
ProgressDescription = "Clone ...";
103104

105+
_cancellation = new CancellationTokenSource();
104106
return Task.Run(() =>
105107
{
106108
var cmd = new Commands.Clone(_pageId, _parentFolder, _remote, _local, _useSSH ? _sshKey : "", _extraArgs, SetProgressDescription);
107-
cmd.CancellationToken = CancelInProgressTokenSource.Token;
109+
cmd.CancellationToken = _cancellation.Token;
108110
if (!cmd.Exec())
109111
return false;
110112

@@ -169,12 +171,18 @@ public override Task<bool> Sure()
169171
});
170172
}
171173

174+
public override void DoCancelInProgress()
175+
{
176+
_cancellation?.Cancel();
177+
}
178+
172179
private string _pageId = string.Empty;
173180
private string _remote = string.Empty;
174181
private bool _useSSH = false;
175182
private string _sshKey = string.Empty;
176183
private string _parentFolder = Preferences.Instance.GitDefaultCloneDir;
177184
private string _local = string.Empty;
178185
private string _extraArgs = string.Empty;
186+
private CancellationTokenSource _cancellation = null;
179187
}
180188
}

‎src/ViewModels/Fetch.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading;
23
using System.Threading.Tasks;
34

45
namespace SourceGit.ViewModels
@@ -63,6 +64,7 @@ public Fetch(Repository repo, Models.Remote preferedRemote = null)
6364
public override Task<bool> Sure()
6465
{
6566
_repo.SetWatcherEnabled(false);
67+
_cancellation = new CancellationTokenSource();
6668

6769
var notags = _repo.Settings.FetchWithoutTags;
6870
var force = _repo.Settings.EnableForceOnFetch;
@@ -74,15 +76,15 @@ public override Task<bool> Sure()
7476
{
7577
SetProgressDescription($"Fetching remote: {remote.Name}");
7678
var cmd = new Commands.Fetch(_repo.FullPath, remote.Name, notags, force, SetProgressDescription);
77-
cmd.CancellationToken = CancelInProgressTokenSource.Token;
79+
cmd.CancellationToken = _cancellation.Token;
7880
cmd.Exec();
7981
}
8082
}
8183
else
8284
{
8385
SetProgressDescription($"Fetching remote: {SelectedRemote.Name}");
8486
var cmd = new Commands.Fetch(_repo.FullPath, SelectedRemote.Name, notags, force, SetProgressDescription);
85-
cmd.CancellationToken = CancelInProgressTokenSource.Token;
87+
cmd.CancellationToken = _cancellation.Token;
8688
cmd.Exec();
8789
}
8890

@@ -96,7 +98,13 @@ public override Task<bool> Sure()
9698
});
9799
}
98100

101+
public override void DoCancelInProgress()
102+
{
103+
_cancellation?.Cancel();
104+
}
105+
99106
private readonly Repository _repo = null;
100107
private bool _fetchAllRemotes;
108+
private CancellationTokenSource _cancellation = null;
101109
}
102110
}

‎src/ViewModels/Popup.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3-
using System.Threading;
43
using System.Threading.Tasks;
54

65
using Avalonia.Threading;
@@ -35,12 +34,6 @@ public bool CanCancelInProgress
3534
set;
3635
} = false;
3736

38-
public CancellationTokenSource CancelInProgressTokenSource
39-
{
40-
get;
41-
set;
42-
} = new CancellationTokenSource();
43-
4437
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode")]
4538
public bool Check()
4639
{
@@ -53,7 +46,7 @@ public bool Check()
5346
public void CancelInProgress()
5447
{
5548
if (CanCancelInProgress)
56-
CancelInProgressTokenSource.Cancel();
49+
DoCancelInProgress();
5750
}
5851

5952
public virtual bool CanStartDirectly()
@@ -66,6 +59,10 @@ public virtual Task<bool> Sure()
6659
return null;
6760
}
6861

62+
public virtual void DoCancelInProgress()
63+
{
64+
}
65+
6966
protected void CallUIThread(Action action)
7067
{
7168
Dispatcher.UIThread.Invoke(action);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /