Skip to content

Commit

Permalink
move thread runner code in generic file and make hash generation thre…
Browse files Browse the repository at this point in the history
…aded
  • Loading branch information
zdimension committed Dec 23, 2019
1 parent 07941fb commit 499afc8
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 166 deletions.
152 changes: 12 additions & 140 deletions SharpBoot/Forms/GenIsoFrm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,15 @@
namespace SharpBoot.Forms
{
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
public partial class GenIsoFrm : Form
public class GenIsoFrm : WorkerFrm
{
public delegate void ChangeProgressBarDelegate(int val, int max);

public delegate void ChangeStatusDelegate(string stat);

public delegate void CloseDelegate();

public bool _usb;

public bool abort;

private bool closeonclick;

public string filesystem = "";


private readonly bool usethread = true;

public GenIsoFrm(string output, bool usb)
{
InitializeComponent();
lblStatus.Text = Strings.Init;
btnAnnul.Text = Strings.Cancel;
Load += GenIsoFrm_Load;
OutputFilepath = output;
_usb = usb;
}
Expand Down Expand Up @@ -68,20 +53,6 @@ public long TotalSize

public Dictionary<string, string> CustomFiles { get; set; }

public event EventHandler GenerationFinished;

protected virtual void OnFinished(EventArgs e)
{
ChangeProgress(100, 100, "");
GenerationFinished?.Invoke(this, e);
BeginInvoke(new CloseDelegate(CloseD));
}

public void CloseD()
{
Close();
}

public void AddImage(ImageLine i)
{
Images.Add(i);
Expand All @@ -90,79 +61,18 @@ public void AddImage(ImageLine i)
Categories.Add(i.Category);
}

public void ChangeProgressBar(int val, int max)
{
if (pbx.InvokeRequired)
{
pbx.Invoke((MethodInvoker) (() =>
{
pbx.Maximum = max;
pbx.Value = val;
}));
}
else
{
pbx.Maximum = max;
pbx.Value = val;
}
}

public void ChangeStatus(string stat)
{
if (lblStatus.InvokeRequired) lblStatus.Invoke((MethodInvoker) (() => lblStatus.Text = stat));
else lblStatus.Text = stat;
}

public void ChangeProgress(int val, int max, string stat)
{
ChangeProgressBar(val, max);

ChangeStatus(stat);
}


protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);


if (bwkISO.IsBusy)
bwkISO.CancelAsync();
}

private void GenIsoFrm_Load(object sender, EventArgs e)
{
if (_usb) Text = Strings.CreatingUSB;
Show();
if (usethread)
bwkISO.RunWorkerAsync();
else
Generate();
}

private void btnAnnul_Click(object sender, EventArgs e)
{
if (closeonclick)
{
DialogResult = DialogResult.Cancel;
Close();
}
else
{
bwkISO.CancelAsync();
SetCancel();
}
}

public void Generate()
public override void DoWork()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(Settings.Default.Lang);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Lang);


var f = Utils.GetTemporaryDirectory();

lblStatus.Text = Strings.Init;
ChangeStatus(Strings.Init);
Thread.Sleep(1000);

var ext = new SevenZipExtractor();
Expand Down Expand Up @@ -245,7 +155,7 @@ public void Generate()
}
}

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand All @@ -268,7 +178,7 @@ public void Generate()

ChangeProgress(0, 100, Strings.ExtractBaseDisk + " 1/6");
ext.Extract(Path.Combine(archs, "basedisk.7z"), isodir);
if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand Down Expand Up @@ -299,7 +209,7 @@ public void Generate()
ChangeProgressBar(60, 100);
Utils.SafeDel(archs);

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand Down Expand Up @@ -352,7 +262,7 @@ public void Generate()
}
}

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand All @@ -369,7 +279,7 @@ public void Generate()

//var itype = new Func<string, EntryType>(fn => Path.GetExtension(fn).ToLower() == ".img" ? EntryType.IMG : EntryType.ISO);

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand Down Expand Up @@ -403,7 +313,7 @@ public void Generate()

ii++;

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand All @@ -412,7 +322,7 @@ public void Generate()

File.WriteAllText(Path.Combine(workingDir, "grub.cfg"), Grub2.GetCode(main));

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand Down Expand Up @@ -457,7 +367,7 @@ public void Generate()

Thread.Sleep(500);

if (bwkISO.CancellationPending)
if (bwkWorker.CancellationPending)
{
abort = true;
return;
Expand Down Expand Up @@ -546,26 +456,6 @@ public void Generate()
ext.Close();
}

private void bwkISO_DoWork(object sender, DoWorkEventArgs e)
{
Generate();
if (abort)
{
closeonclick = true;
SetCancel();
}
}

private void SetCancel()
{
ChangeProgress(0, 100, Strings.OpCancelled);
btnAnnul.Invoke((MethodInvoker) (() =>
{
btnAnnul.Text = Strings.Close;
btnAnnul.DialogResult = DialogResult.Cancel;
}));
}

private void GenF(string f)
{
var iter = 0;
Expand All @@ -582,25 +472,7 @@ private void GenF(string f)
iter++;
}

Invoke((MethodInvoker) (() => OnFinished(EventArgs.Empty)));

Utils.ClrTmp();
}

private void bwkISO_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
abort = true;
SetCancel();
}

if (e.Error != null)
{
if (e.Error is FileNotFoundException exception)
MessageBox.Show("File not found: " + exception.FileName);
else throw new Exception("Error: " + e.Error.Message + "\n", e.Error);
}
}
}
}
22 changes: 22 additions & 0 deletions SharpBoot/Forms/GenericTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpBoot.Forms
{
public sealed class GenericTask : WorkerFrm
{
public GenericTask(string title)
{
Text = title;
}

public Action WorkHandler { get; set; }

public override void DoWork()
{
WorkHandler?.Invoke();
}
}
}
41 changes: 29 additions & 12 deletions SharpBoot/Forms/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private void launchgeniso(bool usb)
var fn = "";
fn = usb ? ((USBFrm) ask).SelectedUSB.Name.ToUpper().Substring(0, 3) : ((AskPath) ask).FileName;
var g = new GenIsoFrm(fn, usb);
g.GenerationFinished += delegate { g_GenerationFinished(g); };
g.WorkFinished += delegate { g_GenerationFinished(g); };

g.Title = txtTitle.Text;
if (usb) g.filesystem = ((USBFrm) ask).TheComboBox.SelectedItem.ToString().RemoveRecommended();
Expand Down Expand Up @@ -513,21 +513,38 @@ private void btnChecksum_Click(object sender, EventArgs e)

private void chksum(string n, Func<string> f)
{
var d = DateTime.Now;
Cursor = Cursors.WaitCursor;

var frm = new GenericTask(n);
var res = "";
frm.WorkHandler = () =>
{
var d = DateTime.Now;
var sb = f();
var sb = f();
var a = DateTime.Now;
var t = a - d;
var a = DateTime.Now;
var t = a - d;
txImInfo.Text = string.Format(Strings.ChkOf, n,
Path.GetFileName(lvIsos.SelectedRows[0].Cells[4].Value.ToString())) + "\r\n";
txImInfo.Text += sb + "\r\n";
/*txImInfo.Text += Strings.CalcIn + " " + t.Hours + "h " + t.Minutes + "m " + (t.TotalMilliseconds / 1000.0) +
"s";*/
txImInfo.Text += string.Format(Strings.CalcIn, t);
Cursor = Cursors.Default;
res = string.Format(Strings.ChkOf, n,
Path.GetFileName(lvIsos.SelectedRows[0].Cells[4].Value.ToString())) + "\r\n";
res += sb + "\r\n";
/*txImInfo.Text += Strings.CalcIn + " " + t.Hours + "h " + t.Minutes + "m " + (t.TotalMilliseconds / 1000.0) +
"s";*/
res += string.Format(Strings.CalcIn, t);
};

frm.WorkFinished += delegate
{
this.InvokeIfRequired(() =>
{
Cursor = Cursors.Default;
txImInfo.Text = res;
});
};

frm.ShowDialog(this);
}

private void theupdate()
Expand Down
Loading

0 comments on commit 499afc8

Please sign in to comment.