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.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Extensions.Logging;
namespace ImageCatalog_2 namespace ImageCatalog_2
{ {
@ -17,20 +18,36 @@ namespace ImageCatalog_2
public ICommand TestCommand { get; } public ICommand TestCommand { get; }
public ICommand AsyncTestCommand { get; } public ICommand AsyncTestCommand { get; }
public ICommand AsyncCancelOperationCommand { get; }
public ICommand ProcessImagesCommand { get; } public ICommand ProcessImagesCommand { get; }
private readonly ITestService _service; private readonly ITestService _service;
public DataModel(ITestService testService) private readonly ILogger<DataModel> _logger;
public DataModel(ITestService testService, ILogger<DataModel> logger)
{ {
_service = testService; _service = testService;
_logger = logger;
TestCommand = new RelayCommand(Test); TestCommand = new RelayCommand(Test);
AsyncTestCommand = new AsyncCommand(TestAsync); AsyncTestCommand = new AsyncCommand(TestAsync);
AsyncCancelOperationCommand = new AsyncCommand(CancelOperation);
ProcessImagesCommand = new AsyncCommand(ProcessImages); ProcessImagesCommand = new AsyncCommand(ProcessImages);
} }
private CancellationTokenSource? _mainToken;
public CancellationTokenSource? MainToken
{
get => _mainToken;
set
{
_mainToken = value;
NotifyPropertyChanged();
}
}
private string _sourcePath; private string _sourcePath;
public string SourcePath public string SourcePath
@ -131,5 +148,20 @@ 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(); ProgressBar1 = new ProgressBar();
CheckBox22 = new CheckBox(); CheckBox22 = new CheckBox();
Label43 = new Label(); Label43 = new Label();
bindingSource1 = new BindingSource(components);
dataModelBindingSource = new BindingSource(components);
TabControl1 = new TabControl(); TabControl1 = new TabControl();
TabPage5 = new TabPage(); TabPage5 = new TabPage();
button1 = new Button(); button1 = new Button();
bindingSource1 = new BindingSource(components);
dataModelBindingSource = new BindingSource(components);
btnTest = new Button(); btnTest = new Button();
GroupBox11 = new GroupBox(); GroupBox11 = new GroupBox();
Panel3 = new Panel(); Panel3 = new Panel();
@ -181,10 +181,10 @@ namespace ImageCatalog
_btnCreaCatalogoAsync = new Button(); _btnCreaCatalogoAsync = new Button();
timer1 = new System.Windows.Forms.Timer(components); timer1 = new System.Windows.Forms.Timer(components);
dataModelBindingSource1 = new BindingSource(components); dataModelBindingSource1 = new BindingSource(components);
TabControl1.SuspendLayout();
TabPage5.SuspendLayout();
((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit(); ((System.ComponentModel.ISupportInitialize)bindingSource1).BeginInit();
((System.ComponentModel.ISupportInitialize)dataModelBindingSource).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).BeginInit();
TabControl1.SuspendLayout();
TabPage5.SuspendLayout();
GroupBox11.SuspendLayout(); GroupBox11.SuspendLayout();
Panel3.SuspendLayout(); Panel3.SuspendLayout();
GroupBox3.SuspendLayout(); GroupBox3.SuspendLayout();
@ -240,6 +240,14 @@ namespace ImageCatalog
Label43.Text = "000"; Label43.Text = "000";
Label43.TextAlign = ContentAlignment.MiddleLeft; Label43.TextAlign = ContentAlignment.MiddleLeft;
// //
// bindingSource1
//
bindingSource1.DataSource = dataModelBindingSource;
//
// dataModelBindingSource
//
dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel);
//
// TabControl1 // TabControl1
// //
TabControl1.Controls.Add(TabPage5); TabControl1.Controls.Add(TabPage5);
@ -283,14 +291,6 @@ namespace ImageCatalog
button1.Text = "Test Async"; button1.Text = "Test Async";
button1.UseVisualStyleBackColor = true; button1.UseVisualStyleBackColor = true;
// //
// bindingSource1
//
bindingSource1.DataSource = dataModelBindingSource;
//
// dataModelBindingSource
//
dataModelBindingSource.DataSource = typeof(ImageCatalog_2.DataModel);
//
// btnTest // btnTest
// //
btnTest.DataBindings.Add(new Binding("Command", bindingSource1, "TestCommand", true)); btnTest.DataBindings.Add(new Binding("Command", bindingSource1, "TestCommand", true));
@ -1698,14 +1698,14 @@ namespace ImageCatalog
// _Button7 // _Button7
// //
_Button7.DataBindings.Add(new Binding("Enabled", bindingSource1, "UiDisabled", true, DataSourceUpdateMode.OnPropertyChanged)); _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.Location = new Point(1168, 296);
_Button7.Margin = new Padding(6, 8, 6, 8); _Button7.Margin = new Padding(6, 8, 6, 8);
_Button7.Name = "_Button7"; _Button7.Name = "_Button7";
_Button7.Size = new Size(416, 99); _Button7.Size = new Size(416, 99);
_Button7.TabIndex = 61; _Button7.TabIndex = 61;
_Button7.Text = "stop creazione"; _Button7.Text = "STOP";
_Button7.Click += Button7_Click;
// //
// _Button5 // _Button5
// //
@ -1826,10 +1826,10 @@ namespace ImageCatalog
Name = "MainForm"; Name = "MainForm";
Text = "Image Catalog"; Text = "Image Catalog";
Load += Form1_Load; Load += Form1_Load;
TabControl1.ResumeLayout(false);
TabPage5.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit(); ((System.ComponentModel.ISupportInitialize)bindingSource1).EndInit();
((System.ComponentModel.ISupportInitialize)dataModelBindingSource).EndInit(); ((System.ComponentModel.ISupportInitialize)dataModelBindingSource).EndInit();
TabControl1.ResumeLayout(false);
TabPage5.ResumeLayout(false);
GroupBox11.ResumeLayout(false); GroupBox11.ResumeLayout(false);
GroupBox11.PerformLayout(); GroupBox11.PerformLayout();
Panel3.ResumeLayout(false); Panel3.ResumeLayout(false);
@ -2218,30 +2218,6 @@ namespace ImageCatalog
private Button _Button7; 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 _Button5; private Button _Button5;
internal Button Button5 internal Button Button5

View file

@ -81,7 +81,7 @@ public partial class MainForm
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
SetTextCallback d = new SetTextCallback(SetText); var d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { target, text }); this.Invoke(d, new object[] { target, text });
} }
else else
@ -114,8 +114,6 @@ public partial class MainForm
SetText(lblFotoTotaliNum, args.Item2.ToString()); SetText(lblFotoTotaliNum, args.Item2.ToString());
} }
/* TODO ERROR: Skipped DefineDirectiveTrivia */
private bool StopAttivo;
private bool WaterSelectColor = false; private bool WaterSelectColor = false;
// Private ContaFotoCuori As Integer // Private ContaFotoCuori As Integer
@ -758,22 +756,20 @@ public partial class MainForm
return NewSize; return NewSize;
} }
private void Button7_Click(object sender, EventArgs e) // private void Button7_Click(object sender, EventArgs e)
{ // {
StopAttivo = true; // try
//MyPool.StopThreadPool(); // {
try // Model.MainToken?.Cancel();
{ // }
_mainToken?.Cancel(); // catch (Exception exception)
} // {
catch (Exception exception) // _logger.LogError(exception.Message);
{ // _logger.LogInformation("Ignora questo errore");
_logger.LogError(exception.Message); // }
_logger.LogInformation("Ignora questo errore"); //
} // unlockUI();
// }
unlockUI();
}
private void Button4_Click(object sender, EventArgs e) 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) private async void Button1_Click(object sender, EventArgs e)
{ {
@ -902,16 +898,16 @@ public partial class MainForm
lockUI(); lockUI();
// Dim timeStart As Date // Dim timeStart As Date
// Dim timeStop As Date // Dim timeStop As Date
_mainToken?.Dispose(); Model.MainToken?.Dispose();
_mainToken = new CancellationTokenSource(); Model.MainToken = new CancellationTokenSource();
var token = _mainToken.Token; var token = Model.MainToken.Token;
// timeStart = TimeOfDay // timeStart = TimeOfDay
FixPaths(); FixPaths();
Label10.Text = "Elaborazione in corso..."; Label10.Text = "Elaborazione in corso...";
lblFotoTotaliNum.Text = "0"; lblFotoTotaliNum.Text = "0";
Label18.Text = "0"; Label18.Text = "0";
Model.SpeedCounter = "-s"; Model.SpeedCounter = "-f/m";
SetPicSettings(Model.SourcePath, Model.DestinationPath); SetPicSettings(Model.SourcePath, Model.DestinationPath);
ProgressBar1.Minimum = 0; ProgressBar1.Minimum = 0;
ProgressBar1.Step = 1; ProgressBar1.Step = 1;
@ -955,8 +951,8 @@ public partial class MainForm
} }
finally finally
{ {
_mainToken?.Dispose(); Model.MainToken?.Dispose();
_mainToken = null; Model.MainToken = null;
timer1.Tick -= Timer1OnTick; 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"> <metadata name="bindingSource1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>586, 17</value> <value>586, 17</value>
</metadata> </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"> <metadata name="dataModelBindingSource.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>120, 17</value> <value>120, 17</value>
</metadata> </metadata>