Merge branch 'release/UpdateWPS_04'
This commit is contained in:
@@ -143,7 +143,6 @@ namespace IOB_WIN_FORM
|
||||
|
||||
lg = LogManager.GetCurrentClassLogger();
|
||||
displayTaskAndLog("AdapterForm Starting");
|
||||
//_ = AdapterForm_Load();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -249,21 +248,65 @@ namespace IOB_WIN_FORM
|
||||
|
||||
|
||||
private CancellationTokenSource _cts;
|
||||
private Task _workerTask;
|
||||
private readonly object _lock = new object(); // Per thread-safety
|
||||
private bool _isSuspended = false;
|
||||
|
||||
// Metodo per avviare il worker (es. nel Form_Load)
|
||||
private void StartWorker()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
// 1. Controllo se è già in esecuzione
|
||||
if (_workerTask != null && !_workerTask.IsCompleted)
|
||||
{
|
||||
lgTrace("Worker già in esecuzione. Richiesta ignorata.");
|
||||
return;
|
||||
}
|
||||
|
||||
_cts = new CancellationTokenSource();
|
||||
// Assegniamo il Task alla variabile per poterne monitorare lo stato
|
||||
_workerTask = Task.Run(() => WorkerLoopAsync(_cts.Token));
|
||||
}
|
||||
#if false
|
||||
_cts = new CancellationTokenSource();
|
||||
// Non usiamo await qui perché vogliamo che il loop giri "per i fatti suoi"
|
||||
_ = Task.Run(() => WorkerLoopAsync(_cts.Token));
|
||||
_ = Task.Run(() => WorkerLoopAsync(_cts.Token));
|
||||
#endif
|
||||
}
|
||||
|
||||
// Metodo per fermare tutto (es. nel Form_Closing o Dispose)
|
||||
private void StopWorker()
|
||||
private async Task StopWorker()
|
||||
{
|
||||
if (_cts == null) return;
|
||||
|
||||
_cts.Cancel();
|
||||
|
||||
try
|
||||
{
|
||||
// Opzionale: attendi che il task finisca davvero prima di fare il dispose
|
||||
if (_workerTask != null)
|
||||
{
|
||||
await _workerTask;
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { /* Normale amministrazione */ }
|
||||
finally
|
||||
{
|
||||
if (_cts != null)
|
||||
{
|
||||
_cts.Dispose();
|
||||
_cts = null;
|
||||
}
|
||||
if (_workerTask != null)
|
||||
{
|
||||
_workerTask = null;
|
||||
}
|
||||
}
|
||||
#if false
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
_cts?.Dispose();
|
||||
#endif
|
||||
}
|
||||
|
||||
private async Task WorkerLoopAsync(CancellationToken ct)
|
||||
@@ -283,6 +326,10 @@ namespace IOB_WIN_FORM
|
||||
lgError("Errore durante i task: " + ex.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lgTrace("Task sospeso: NON esegue task specifici");
|
||||
}
|
||||
|
||||
// Calcolo del delay dinamico da Timers.MsVHF
|
||||
int currentDelay = _isSuspended ? 10 * IOBConfFull.General.Timers.MsVHF : IOBConfFull.General.Timers.MsVHF;
|
||||
@@ -292,6 +339,7 @@ namespace IOB_WIN_FORM
|
||||
// senza dover aspettare la fine del timer, velocizzando la chiusura.
|
||||
await Task.Delay(currentDelay, ct);
|
||||
}
|
||||
lgInfo("Worker interrotto per cancellation token.");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -381,10 +429,12 @@ namespace IOB_WIN_FORM
|
||||
}
|
||||
}
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// Timer principale di esecuzione tasks in background
|
||||
/// </summary>
|
||||
protected System.Threading.Timer workerTimer;
|
||||
protected System.Threading.Timer workerTimer;
|
||||
#endif
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
@@ -1158,10 +1208,15 @@ namespace IOB_WIN_FORM
|
||||
restart.Enabled = true;
|
||||
// 2026.01.02 fix timers al restart
|
||||
gather.Enabled = true;
|
||||
// gestione worker
|
||||
_isSuspended = false;
|
||||
StartWorker();
|
||||
#if false
|
||||
if (workerTimer != null)
|
||||
{
|
||||
workerTimer.Change(0, IOBConfFull.General.Timers.MsVHF);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
displayTaskAndLog("Start Timers", true);
|
||||
// inizializzo le scadenze dei timers...
|
||||
@@ -1231,6 +1286,7 @@ namespace IOB_WIN_FORM
|
||||
public void fermaAdapter(bool tryRestart, bool forceDequeue, bool updateForm)
|
||||
{
|
||||
lgInfo($"fermaAdapter | tryRestart: {tryRestart} | forceDequeue: {forceDequeue} | updateForm: {updateForm}");
|
||||
//fermaTutto(false, tryRestart, forceDequeue, updateForm);
|
||||
fermaTutto(false, tryRestart, forceDequeue, updateForm);
|
||||
}
|
||||
|
||||
@@ -1653,7 +1709,10 @@ namespace IOB_WIN_FORM
|
||||
displTimer?.Dispose();
|
||||
gather?.Stop();
|
||||
gather?.Dispose();
|
||||
workerTimer?.Dispose();
|
||||
#if false
|
||||
workerTimer?.Dispose();
|
||||
#endif
|
||||
StopWorker();
|
||||
}
|
||||
|
||||
private void displTimer_Tick(object sender, EventArgs e)
|
||||
@@ -1679,14 +1738,18 @@ namespace IOB_WIN_FORM
|
||||
stop.Enabled = false;
|
||||
start.Enabled = true;
|
||||
restart.Enabled = false;
|
||||
_isSuspended = true;
|
||||
if (stopTimer)
|
||||
{
|
||||
gather.Enabled = false;
|
||||
_ = StopWorker();
|
||||
#if false
|
||||
// FixMe ToDo: eliminare workerTimer !!!
|
||||
if (workerTimer != null)
|
||||
{
|
||||
workerTimer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
newDisplayData currDispData = new newDisplayData();
|
||||
|
||||
@@ -24,3 +24,4 @@ STARTLIST=3019
|
||||
;STARTLIST=SIMUL_05
|
||||
|
||||
MAXCNC=10
|
||||
|
||||
|
||||
Reference in New Issue
Block a user