Show a progress dialog while resolving referenced components
"Trova componenti referenziati" ran the recursive BFS synchronously on the UI thread with no feedback. Runs it on a background task instead and shows a modal dialog with a percentage bar, updated as each .asm in the queue is processed. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bb99c4f4c5
commit
ee408219bd
2 files changed
+55
-4
No files matched your search
+18
-4
@@ -636,7 +636,7 @@ public partial class Form1 : Form
|
||||
/// accoda per essere scansionati finché non restano solo PRT (foglie) o codici non presenti
|
||||
/// nell'indice. Usata dal bottone "Trova componenti referenziati".</summary>
|
||||
private (List<(string Codice, string Tipo, string Percorso)> Trovati, List<string> NonTrovati) RisolviAlberoComponenti(
|
||||
IEnumerable<string> asmIniziali)
|
||||
IEnumerable<string> asmIniziali, IProgress<(int Fatti, int Totale)>? progress = null)
|
||||
{
|
||||
var daScansionare = new Queue<string>(asmIniziali);
|
||||
var codiciVisti = new HashSet<string>(
|
||||
@@ -655,9 +655,11 @@ public partial class Form1 : Form
|
||||
+ "ORDER BY tipo_file, data_modifica DESC";
|
||||
var pCodice = command.Parameters.Add("@codice", Microsoft.Data.Sqlite.SqliteType.Text);
|
||||
|
||||
int processati = 0;
|
||||
while (daScansionare.Count > 0)
|
||||
{
|
||||
var asm = daScansionare.Dequeue();
|
||||
processati++;
|
||||
foreach (var codice in AssemblyComponentScanner.EstraiCodiciReferenziati(asm))
|
||||
{
|
||||
if (!codiciVisti.Add(codice))
|
||||
@@ -696,12 +698,13 @@ public partial class Form1 : Form
|
||||
}
|
||||
}
|
||||
}
|
||||
progress?.Report((processati, processati + daScansionare.Count));
|
||||
}
|
||||
|
||||
return (trovati, codiciNonTrovati);
|
||||
}
|
||||
|
||||
private void TrovaPrtReferenziati()
|
||||
private async void TrovaPrtReferenziati()
|
||||
{
|
||||
if (_ultimaCartellaRaw is null || !Directory.Exists(_ultimaCartellaRaw))
|
||||
return;
|
||||
@@ -714,8 +717,19 @@ public partial class Form1 : Form
|
||||
|
||||
// L'assieme generale è già in raw come qualsiasi altro assieme importato (vedi
|
||||
// CreaCartellaRaw), quindi rientra qui senza bisogno di un caso speciale.
|
||||
var asmIniziali = Directory.GetFiles(_ultimaCartellaRaw).Where(f => Path.GetFileName(f).Contains(".asm."));
|
||||
var (trovati, codiciNonTrovati) = RisolviAlberoComponenti(asmIniziali);
|
||||
var asmIniziali = Directory.GetFiles(_ultimaCartellaRaw).Where(f => Path.GetFileName(f).Contains(".asm.")).ToList();
|
||||
|
||||
using var progressForm = new ProgressoComponentiForm();
|
||||
var progress = new Progress<(int Fatti, int Totale)>(p => progressForm.Aggiorna(p.Fatti, p.Totale));
|
||||
var task = Task.Run(() => RisolviAlberoComponenti(asmIniziali, progress));
|
||||
progressForm.Shown += async (_, _) =>
|
||||
{
|
||||
await task;
|
||||
progressForm.Close();
|
||||
};
|
||||
progressForm.ShowDialog(this);
|
||||
|
||||
var (trovati, codiciNonTrovati) = await task;
|
||||
|
||||
int numPrt = trovati.Count(r => r.Tipo == "prt");
|
||||
int numAsm = trovati.Count(r => r.Tipo == "asm");
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace CadSearchGui;
|
||||
|
||||
public sealed class ProgressoComponentiForm : Form
|
||||
{
|
||||
private readonly Label _lbl = new();
|
||||
private readonly ProgressBar _bar = new();
|
||||
|
||||
public ProgressoComponentiForm()
|
||||
{
|
||||
Text = "Ricerca componenti referenziati in corso...";
|
||||
FormBorderStyle = FormBorderStyle.FixedDialog;
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
MinimizeBox = false;
|
||||
MaximizeBox = false;
|
||||
ControlBox = false;
|
||||
ClientSize = new Size(400, 88);
|
||||
|
||||
_lbl.Text = "Preparazione...";
|
||||
_lbl.AutoSize = true;
|
||||
_lbl.Location = new Point(16, 16);
|
||||
|
||||
_bar.Location = new Point(16, 44);
|
||||
_bar.Size = new Size(368, 24);
|
||||
_bar.Minimum = 0;
|
||||
_bar.Maximum = 100;
|
||||
|
||||
Controls.Add(_lbl);
|
||||
Controls.Add(_bar);
|
||||
}
|
||||
|
||||
public void Aggiorna(int fatti, int totale)
|
||||
{
|
||||
int percento = totale > 0 ? (int)Math.Clamp(100.0 * fatti / totale, 0, 100) : 0;
|
||||
_bar.Value = percento;
|
||||
_lbl.Text = $"{fatti} / {totale} componenti analizzati ({percento}%)";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user