Skip to content

Commit

Permalink
Improved more invoke methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed Jun 4, 2016
1 parent d06a546 commit 3984147
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
37 changes: 37 additions & 0 deletions AA2Install/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public static DialogResult InvokeMessageBox(this Form form, string text, string
return MessageBox.Show(form, text, caption, buttons, icon);
}

/// <summary>
/// Invokes a control with a method only if the invoke is required.
/// </summary>
/// <param name="control">The control to invoke.</param>
/// <param name="action">The action to invoke with.</param>
public static void HybridInvoke(this Control control, Action action)
{
if (control.InvokeRequired)
control.BeginInvoke(action);
else
action();
}

/// <summary>
/// Keeps a thread responsive while waiting for a blocking call.
/// </summary>
Expand Down Expand Up @@ -69,5 +82,29 @@ public static void SemiAsyncWait(this Task task)
while (!(task.IsCompleted || task.IsCanceled || task.IsFaulted))
Application.DoEvents();
}

/// <summary>
/// Keeps a thread responsive while waiting for a blocking call.
/// </summary>
/// <param name="task">The task to wait for.</param>
public static T SemiAsyncWait<T>(this Task<T> task)
{
if (task.Status == TaskStatus.Created)
task.Start();

while (!(task.IsCompleted || task.IsCanceled || task.IsFaulted))
Application.DoEvents();

return task.Result;
}

/// <summary>
/// Keeps a thread responsive while waiting for a blocking call.
/// </summary>
/// <param name="task">The task to wait for.</param>
public static T SemiAsyncWait<T>(this Func<T> func)
{
return new Task<T>(func).SemiAsyncWait();
}
}
}
66 changes: 30 additions & 36 deletions AA2Install/formMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,15 @@ public bool inject(bool createBackup = false, bool checkConflicts = true, bool s
}

SevenZipBase.ProgressUpdatedEventArgs progress = (i) => {
this.prgMinor.GetCurrentParent().Invoke((MethodInvoker)delegate {
this.prgMinor.GetCurrentParent().HybridInvoke(() => {
prgMinor.Value = i;
});
updateStatus("(" + index + "/" + combined.Count + ") Verifying " + m.Name + " (" + i + "%)...", LogIcon.Processing, false, true);
};

s.ProgressUpdated += progress;

bool result = s.TestArchive();
bool result = new Func<bool>(() => s.TestArchive()).SemiAsyncWait();

s.ProgressUpdated -= progress;

Expand Down Expand Up @@ -696,7 +696,7 @@ public bool inject(bool createBackup = false, bool checkConflicts = true, bool s
string name = "";

updateProgress = (i) => {
this.Invoke((MethodInvoker)delegate {
prgMinor.GetCurrentParent().HybridInvoke(() => {
prgMinor.Value = i;
});
updateStatus("(" + index + "/" + combined.Count + ") Extracting " + name + " (" + i + "%)...", LogIcon.Processing, false, true);
Expand Down Expand Up @@ -827,10 +827,10 @@ public bool inject(bool createBackup = false, bool checkConflicts = true, bool s

bb.ProgressChanged += ((s, e) =>
{
this.Invoke((MethodInvoker)delegate {
prgMinor.GetCurrentParent().HybridInvoke(() => {
prgMinor.Value = e.ProgressPercentage;
baseUpdateStatus("(" + ii + "/" + ppList.Count + ") Reverting " + b.ppFile + " (" + e.ProgressPercentage + "%)...", LogIcon.Processing, false, true);
});
updateStatus("(" + ii + "/" + ppList.Count + ") Reverting " + b.ppFile + " (" + e.ProgressPercentage + "%)...", LogIcon.Processing, false, true);
});

bb.SemiAsyncWait();
Expand Down Expand Up @@ -924,7 +924,7 @@ public bool inject(bool createBackup = false, bool checkConflicts = true, bool s

bb.ProgressChanged += ((s, e) =>
{
this.Invoke((MethodInvoker)delegate
prgMinor.GetCurrentParent().HybridInvoke(() =>
{ prgMinor.Value = e.ProgressPercentage; });

updateStatus("(" + (index + 1) + "/" + initial + ") Injecting " + b.ppFile + " (" + e.ProgressPercentage + "%)...", LogIcon.Processing, false, true);
Expand Down Expand Up @@ -958,10 +958,10 @@ public bool inject(bool createBackup = false, bool checkConflicts = true, bool s
updateStatus("(" + ind + "/" + tempBackup.Count + ") Archiving backup of " + s + " (0%)...", LogIcon.Processing);

updateProgress = (i) => {
this.Invoke((MethodInvoker)delegate {
prgMinor.GetCurrentParent().HybridInvoke(() => {
prgMinor.Value = i;
updateStatus("(" + ind + "/" + tempBackup.Count + ") Archiving backup of " + s + " (" + i + "%)...", LogIcon.Processing, false, true);
});
updateStatus("(" + ind + "/" + tempBackup.Count + ") Archiving backup of " + s + " (" + i + "%)...", LogIcon.Processing, false, true);
};

_7z.ProgressUpdated += updateProgress;
Expand Down Expand Up @@ -1702,47 +1702,41 @@ public TimeSpan getTimeSinceLastCheck()

private bool showTime = false;

private void baseUpdateStatus(string entry, LogIcon icon = LogIcon.Ready, bool displayTime = true, bool onlyStatusBar = false)
private void updateStatus(string entry, LogIcon icon = LogIcon.Ready, bool displayTime = true, bool onlyStatusBar = false)
{
if (statusUpdated != null)
statusUpdated(entry);
if (onlyStatusBar)
{
labelStatus.Text = entry;
labelStatus.GetCurrentParent().HybridInvoke(() =>
labelStatus.Text = entry);
}
else
{
Console.ProgramLog.Add(new LogEntry(entry, icon));

if (lsvLog.Items.Count > 0 && showTime)
{
lsvLog.Items[lsvLog.Items.Count - 1].SubItems.Add((getTimeSinceLastCheck().TotalMilliseconds / 1000).ToString("F2") + "s");
}
showTime = displayTime;
switch (icon)

lsvLog.HybridInvoke(() =>
{
case LogIcon.Error:
case LogIcon.Warning:
lsvLog.Items.Add(entry, (int)icon);
break;
default:
lsvLog.Items.Add(entry, (int)icon);
labelStatus.Text = entry;
break;
}
if (lsvLog.Items.Count > 0 && showTime)
{
lsvLog.Items[lsvLog.Items.Count - 1].SubItems.Add((getTimeSinceLastCheck().TotalMilliseconds / 1000).ToString("F2") + "s");
}
showTime = displayTime;
switch (icon)
{
case LogIcon.Error:
case LogIcon.Warning:
lsvLog.Items.Add(entry, (int)icon);
break;
default:
lsvLog.Items.Add(entry, (int)icon);
labelStatus.Text = entry;
break;
}
});
}

}

public void updateStatus(string entry, LogIcon icon = LogIcon.Ready, bool displayTime = true, bool onlyStatusBar = false)
{
if (this.InvokeRequired)
this.Invoke((MethodInvoker)delegate {
baseUpdateStatus(entry, icon, displayTime, onlyStatusBar);
});
else
baseUpdateStatus(entry, icon, displayTime, onlyStatusBar);
}
#endregion

#region Modpacks
Expand Down

0 comments on commit 3984147

Please sign in to comment.