Extract files in a seperate thread

This commit is contained in:
KillzXGaming 2020-03-17 16:31:43 -04:00
parent b11e913240
commit 08fee34f07

View file

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Toolbox.Library.Forms;
using System.Windows.Forms;
using System.IO;
@ -90,11 +90,18 @@ namespace Toolbox.Library
progressBar.StartPosition = FormStartPosition.CenterScreen;
progressBar.Show();
var Collection = TreeViewExtensions.Collect(Nodes);
Thread Thread = new Thread((ThreadStart)(() =>
{
var Collection = TreeViewExtensions.Collect(Nodes).ToList();
Console.WriteLine($"Collection {Collection.Count}");
int Curfile = 0;
foreach (TreeNode node in Collection)
{
if (progressBar.IsDisposed || progressBar.Disposing) {
break;
}
ArchiveFileInfo file = null;
if (node.Tag != null && node.Tag is ArchiveFileInfo)
@ -120,9 +127,16 @@ namespace Toolbox.Library
var path = $"{overridePath}/{FilePath}";
if (progressBar.InvokeRequired)
{
progressBar.Invoke((MethodInvoker)delegate {
// Running on the UI thread
progressBar.Task = $"Extracting File {FileName}";
progressBar.Value = (Curfile++ * 100) / Collection.Count();
progressBar.Value = (Curfile++ * 100) / Collection.Count;
progressBar.Refresh();
});
}
CreateDirectoryIfExists($"{path}");
filesExtracted.Add($"{path}");
@ -137,9 +151,23 @@ namespace Toolbox.Library
}
}
if (progressBar.InvokeRequired)
{
progressBar.Invoke((MethodInvoker)delegate {
progressBar.Value = 100;
progressBar.Refresh();
progressBar.Close();
});
}
else
{
progressBar.Value = 100;
progressBar.Refresh();
progressBar.Close();
}
}));
Thread.Start();
return filesExtracted.ToArray();
}