Async stop

This commit is contained in:
Marco 2025-07-29 11:07:49 +02:00
commit 80fcaa6fd0
4 changed files with 81 additions and 71 deletions

View file

@ -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");
}
}
}
}

View file

@ -41,11 +41,11 @@ namespace ImageCatalog
ProgressBar1 = new ProgressBar();
CheckBox22 = new CheckBox();
Label43 = new Label();
bindingSource1 = new BindingSource(components);
dataModelBindingSource = new BindingSource(components);
TabControl1 = new TabControl();
TabPage5 = new TabPage();
button1 = new Button();
bindingSource1 = new BindingSource(components);
dataModelBindingSource = new BindingSource(components);
btnTest = new Button();
GroupBox11 = new GroupBox();
Panel3 = new Panel();
@ -181,10 +181,10 @@ namespace ImageCatalog
_btnCreaCatalogoAsync = new Button();
timer1 = new System.Windows.Forms.Timer(components);
dataModelBindingSource1 = new BindingSource(components);
TabControl1.SuspendLayout();
TabPage5.SuspendLayout();
((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit();
((System.ComponentModel.ISupportInitialize)dataModelBindingSource).BeginInit();
TabControl1.SuspendLayout();
TabPage5.SuspendLayout();
GroupBox11.SuspendLayout();
Panel3.SuspendLayout();
GroupBox3.SuspendLayout();
@ -240,6 +240,14 @@ namespace ImageCatalog
Label43.Text = "000";
Label43.TextAlign = ContentAlignment.MiddleLeft;
//
// bindingSource1
//
bindingSource1.DataSource = dataModelBindingSource;
//
// dataModelBindingSource
//
dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel);
//
// TabControl1
//
TabControl1.Controls.Add(TabPage5);
@ -283,14 +291,6 @@ namespace ImageCatalog
button1.Text = "Test Async";
button1.UseVisualStyleBackColor = true;
//
// bindingSource1
//
bindingSource1.DataSource = dataModelBindingSource;
//
// dataModelBindingSource
//
dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel);
//
// btnTest
//
btnTest.DataBindings.Add(new Binding("Command", bindingSource1, "TestCommand", true));
@ -1698,14 +1698,14 @@ namespace ImageCatalog
// _Button7
//
_Button7.DataBindings.Add(new Binding("Enabled", bindingSource1, "UiDisabled", true, DataSourceUpdateMode.OnPropertyChanged));
_Button7.Font = new Font("Microsoft Sans Serif", 14.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
_Button7.DataBindings.Add(new Binding("Command", bindingSource1, "AsyncCancelOperationCommand", true));
_Button7.Font = new Font("Microsoft Sans Serif", 14.25F, FontStyle.Bold, GraphicsUnit.Point, 0);
_Button7.Location = new Point(1168, 296);
_Button7.Margin = new Padding(6, 8, 6, 8);
_Button7.Name = "_Button7";
_Button7.Size = new Size(416, 99);
_Button7.TabIndex = 61;
_Button7.Text = "stop creazione";
_Button7.Click += Button7_Click;
_Button7.Text = "STOP";
//
// _Button5
//
@ -1826,10 +1826,10 @@ namespace ImageCatalog
Name = "MainForm";
Text = "Image Catalog";
Load += Form1_Load;
TabControl1.ResumeLayout(false);
TabPage5.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit();
((System.ComponentModel.ISupportInitialize)dataModelBindingSource).EndInit();
TabControl1.ResumeLayout(false);
TabPage5.ResumeLayout(false);
GroupBox11.ResumeLayout(false);
GroupBox11.PerformLayout();
Panel3.ResumeLayout(false);
@ -2216,31 +2216,7 @@ namespace ImageCatalog
}
}
private Button _Button7;
internal Button Button7
{
[MethodImpl(MethodImplOptions.Synchronized)]
get
{
return _Button7;
}
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
if (_Button7 != null)
{
_Button7.Click -= Button7_Click;
}
_Button7 = value;
if (_Button7 != null)
{
_Button7.Click += Button7_Click;
}
}
}
private Button _Button7;
private Button _Button5;

View file

@ -81,7 +81,7 @@ public partial class MainForm
{
if (InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
var d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { target, text });
}
else
@ -113,9 +113,7 @@ public partial class MainForm
SetText(Label10, args.Item1);
SetText(lblFotoTotaliNum, args.Item2.ToString());
}
/* TODO ERROR: Skipped DefineDirectiveTrivia */
private bool StopAttivo;
private bool WaterSelectColor = false;
// Private ContaFotoCuori As Integer
@ -758,22 +756,20 @@ public partial class MainForm
return NewSize;
}
private void Button7_Click(object sender, EventArgs e)
{
StopAttivo = true;
//MyPool.StopThreadPool();
try
{
_mainToken?.Cancel();
}
catch (Exception exception)
{
_logger.LogError(exception.Message);
_logger.LogInformation("Ignora questo errore");
}
unlockUI();
}
// private void Button7_Click(object sender, EventArgs e)
// {
// try
// {
// Model.MainToken?.Cancel();
// }
// catch (Exception exception)
// {
// _logger.LogError(exception.Message);
// _logger.LogInformation("Ignora questo errore");
// }
//
// unlockUI();
// }
private void Button4_Click(object sender, EventArgs e)
{
@ -894,7 +890,7 @@ public partial class MainForm
{
}
private CancellationTokenSource? _mainToken;
//private CancellationTokenSource? _mainToken;
private async void Button1_Click(object sender, EventArgs e)
{
@ -902,16 +898,16 @@ public partial class MainForm
lockUI();
// Dim timeStart As Date
// Dim timeStop As Date
_mainToken?.Dispose();
_mainToken = new CancellationTokenSource();
var token = _mainToken.Token;
Model.MainToken?.Dispose();
Model.MainToken = new CancellationTokenSource();
var token = Model.MainToken.Token;
// timeStart = TimeOfDay
FixPaths();
Label10.Text = "Elaborazione in corso...";
lblFotoTotaliNum.Text = "0";
Label18.Text = "0";
Model.SpeedCounter = "-s";
Model.SpeedCounter = "-f/m";
SetPicSettings(Model.SourcePath, Model.DestinationPath);
ProgressBar1.Minimum = 0;
ProgressBar1.Step = 1;
@ -955,8 +951,8 @@ public partial class MainForm
}
finally
{
_mainToken?.Dispose();
_mainToken = null;
Model.MainToken?.Dispose();
Model.MainToken = null;
timer1.Tick -= Timer1OnTick;
}

View file

@ -120,6 +120,12 @@
<metadata name="bindingSource1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>586, 17</value>
</metadata>
<metadata name="bindingSource1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>586, 17</value>
</metadata>
<metadata name="dataModelBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>120, 17</value>
</metadata>
<metadata name="dataModelBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>120, 17</value>
</metadata>