Async stop
This commit is contained in:
parent
1cd71c54fc
commit
80fcaa6fd0
4 changed files with 81 additions and 71 deletions
|
|
@ -8,6 +8,7 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ImageCatalog_2
|
||||
{
|
||||
|
|
@ -17,20 +18,36 @@ namespace ImageCatalog_2
|
|||
public ICommand TestCommand { get; }
|
||||
|
||||
public ICommand AsyncTestCommand { get; }
|
||||
public ICommand AsyncCancelOperationCommand { get; }
|
||||
|
||||
public ICommand ProcessImagesCommand { get; }
|
||||
|
||||
private readonly ITestService _service;
|
||||
public DataModel(ITestService testService)
|
||||
private readonly ILogger<DataModel> _logger;
|
||||
public DataModel(ITestService testService, ILogger<DataModel> logger)
|
||||
{
|
||||
_service = testService;
|
||||
_logger = logger;
|
||||
|
||||
TestCommand = new RelayCommand(Test);
|
||||
AsyncTestCommand = new AsyncCommand(TestAsync);
|
||||
|
||||
AsyncCancelOperationCommand = new AsyncCommand(CancelOperation);
|
||||
|
||||
ProcessImagesCommand = new AsyncCommand(ProcessImages);
|
||||
}
|
||||
|
||||
private CancellationTokenSource? _mainToken;
|
||||
|
||||
public CancellationTokenSource? MainToken
|
||||
{
|
||||
get => _mainToken;
|
||||
set
|
||||
{
|
||||
_mainToken = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private string _sourcePath;
|
||||
|
||||
public string SourcePath
|
||||
|
|
@ -130,6 +147,21 @@ namespace ImageCatalog_2
|
|||
{
|
||||
|
||||
}
|
||||
|
||||
private async Task CancelOperation()
|
||||
{
|
||||
try
|
||||
{
|
||||
await MainToken?.CancelAsync();
|
||||
|
||||
UiEnabled = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e.Message, "Error canceling the token");
|
||||
_logger.LogInformation("Ignora questo errore");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue