Skip to content
This repository has been archived by the owner on Dec 22, 2019. It is now read-only.

Commit

Permalink
Seperate View component from Task component
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthiee committed Aug 11, 2017
1 parent 5918711 commit ce828fe
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 57 deletions.
16 changes: 8 additions & 8 deletions UpdateLib/UpdateLib/Tasks/CheckForUpdatedItemsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public CheckForUpdatedItemsTask(UpdateFile update, HashCacheFile cache)
protected override void DoWork()
{
foreach (DirectoryEntry dir in m_updateFile.Folders)
Enqueue(new Action<DirectoryEntry>(RecursiveFileCheck), dir);
Enqueue(new Action<DirectoryEntry>(CheckFiles), dir);

foreach (DirectoryEntry dir in m_updateFile.Registry)
Enqueue(new Action<DirectoryEntry>(RecursiveRegCheck), dir);
Enqueue(new Action<DirectoryEntry>(CheckRegister), dir);

AwaitWorkers();

Result = m_updateFile.FileCount > 0 || m_updateFile.RegistryKeyCount > 0;
}

private void RecursiveFileCheck(DirectoryEntry dir)
private void CheckFiles(DirectoryEntry dir)
{
dir.Items.RemoveAll(fe =>
{
Expand All @@ -53,13 +53,13 @@ private void RecursiveFileCheck(DirectoryEntry dir)
foreach (DirectoryEntry subDir in dir.Directories)
{
if (--left == 0)
RecursiveFileCheck(subDir);
CheckFiles(subDir);
else
Enqueue(new Action<DirectoryEntry>(RecursiveFileCheck), subDir);
Enqueue(new Action<DirectoryEntry>(CheckFiles), subDir);
}
}

private void RecursiveRegCheck(DirectoryEntry dir)
private void CheckRegister(DirectoryEntry dir)
{
dir.Items.RemoveAll(entry =>
{
Expand All @@ -77,9 +77,9 @@ private void RecursiveRegCheck(DirectoryEntry dir)
foreach (DirectoryEntry subDir in dir.Directories)
{
if (--left == 0)
RecursiveRegCheck(subDir);
CheckRegister(subDir);
else
Enqueue(new Action<DirectoryEntry>(RecursiveRegCheck), dir);
Enqueue(new Action<DirectoryEntry>(CheckRegister), dir);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions UpdateLib/UpdateLib/Tasks/CheckForUpdatesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected override void DoWork()

Updater updater = Updater.Instance;

if (!NetworkUtils.HasConnection()) throw new WebException("No internet available", WebExceptionStatus.ConnectFailure);

// Getting the file name from the url
string localFile = $@"{IOUtils.AppDataPath}\Update.xml";

Expand Down
8 changes: 1 addition & 7 deletions UpdateLib/UpdateLib/Tasks/DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ public class DownloadTask : UpdatableTask
private ManualResetEvent wait;

public FileEntry Entry { get; private set; }

public DownloadTask(ListViewItem item, FileEntry entry)
: this(entry)
{
Item = item;
}


public DownloadTask(FileEntry entry)
{
Entry = entry;
Expand Down
2 changes: 0 additions & 2 deletions UpdateLib/UpdateLib/Tasks/UpdatableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace MatthiWare.UpdateLib.Tasks
{
public abstract class UpdatableTask : AsyncTask
{
public ListViewItem Item { get; set; }

private new void Start()
{
base.Start();
Expand Down
11 changes: 2 additions & 9 deletions UpdateLib/UpdateLib/Tasks/UpdateRegistryTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ public class UpdateRegistryTask : UpdatableTask
{

public IEnumerable<RegistryKeyEntry> Keys { get; set; }



private List<RollbackData> cachedUpdates = new List<RollbackData>();

public UpdateRegistryTask(ListViewItem item, IEnumerable<RegistryKeyEntry> keys)
: this(keys)
{
Item = item;
}


public UpdateRegistryTask(IEnumerable<RegistryKeyEntry> keys)
{
Keys = keys;
Expand Down
4 changes: 3 additions & 1 deletion UpdateLib/UpdateLib/Tasks/WorkerScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ public static WorkerScheduler Instance
private readonly ManualResetEvent m_waitForAvailableWorker;
private readonly object sync = new object();

public const long MIN_WORKERS = 8;

#endregion

private WorkerScheduler()
{
MAX_WORKERS = Environment.ProcessorCount;
MAX_WORKERS = Math.Max(Environment.ProcessorCount, MIN_WORKERS);
m_taskQueue = new ConcurrentQueue<AsyncTask>();
m_dispatcherTask = AsyncTaskFactory.From(new Action(Dispatcher), null);
m_waitForAvailableWorker = new ManualResetEvent(true);
Expand Down
65 changes: 35 additions & 30 deletions UpdateLib/UpdateLib/UI/Components/UpdatePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public partial class UpdatePage : UserControl, IWizardPage
public event EventHandler PageUpdate;

private AtomicInteger amountToDownload = new AtomicInteger();
private List<UpdatableTask> m_updateTasks = new List<UpdatableTask>();
private Dictionary<UpdatableTask, ListViewItem> m_items = new Dictionary<UpdatableTask, ListViewItem>();
private bool hasRegTask = false;

public UpdatePage(UpdaterForm parent)
Expand Down Expand Up @@ -85,15 +85,14 @@ private void AddDirectoryToListView(IEnumerable<FileEntry> files)
{
foreach (FileEntry entry in files)
{

ListViewItem item = new ListViewItem(new string[] { entry.Name, "Ready to download", "0%", entry.Description, Updater.Instance.Converter.Convert(entry.DestinationLocation) });
item.Tag = entry;

DownloadTask task = new DownloadTask(item, entry);
DownloadTask task = new DownloadTask(entry);
task.TaskProgressChanged += Task_TaskProgressChanged;
task.TaskCompleted += Task_TaskCompleted;

m_updateTasks.Add(task);
m_items.Add(task, item);

lvItems.Items.Add(item);
}
Expand All @@ -109,11 +108,11 @@ private void AddRegistryToListView(IEnumerable<RegistryKeyEntry> keys)

ListViewItem item = new ListViewItem(new string[] { "Update registry", "Waiting for other tasks to complete", "0%", "Applies changes to the registry" });

UpdateRegistryTask task = new UpdateRegistryTask(item, keys);
UpdateRegistryTask task = new UpdateRegistryTask(keys);
task.TaskProgressChanged += Task_TaskProgressChanged;
task.TaskCompleted += Task_TaskCompleted;

m_updateTasks.Add(task);
m_items.Add(task, item);

lvItems.Items.Add(item);
}
Expand All @@ -123,44 +122,47 @@ public void StartUpdate()
IsBusy = true;
PageUpdate?.Invoke(this, new EventArgs());

IEnumerable<DownloadTask> downloadTasks = m_updateTasks.Select(x => x as DownloadTask).NotNull();
var items = m_items.Where(x => (x.Key as DownloadTask != null));

foreach (DownloadTask task in downloadTasks)
foreach (var kvp in items)
{
SetImageKey(task.Item, "status_download");
SetSubItemText(task.Item.SubItems[1], "Downloading..");
SetImageKey(kvp.Value, "status_download");
SetSubItemText(kvp.Value.SubItems[1], "Downloading..");

task.Start();
kvp.Key.Start();
}


if (hasRegTask && downloadTasks.Count() == 0)
if (hasRegTask && items.Count() == 0)
StartRegUpdate();

}

private void StartRegUpdate()
{
UpdateRegistryTask task = m_updateTasks.Select(x => x as UpdateRegistryTask).NotNull().FirstOrDefault();
var kvp = m_items.Where(x => (x.Key as UpdateRegistryTask) != null).NotNull().FirstOrDefault();

if (task == null)
if (kvp.Key == null || kvp.Value == null)
return;

SetImageKey(task.Item, "status_download");
SetSubItemText(task.Item.SubItems[1], "Updating..");
var view = kvp.Value;

SetImageKey(view, "status_download");
SetSubItemText(view.SubItems[1], "Updating..");

task.Start();
kvp.Key.Start();

}

private void Task_TaskCompleted(object sender, AsyncCompletedEventArgs e)
{
UpdatableTask task = (UpdatableTask)sender;
var task = (UpdatableTask)sender;
var view = m_items[task];

if (e.Cancelled)
{
SetSubItemText(task.Item.SubItems[1], "Rolled back");
SetImageKey(task.Item, "status_warning");
SetSubItemText(view.SubItems[1], "Rolled back");
SetImageKey(view, "status_warning");

return;
}
Expand All @@ -172,14 +174,14 @@ private void Task_TaskCompleted(object sender, AsyncCompletedEventArgs e)

Updater.Instance.Logger.Error(nameof(UpdatePage), nameof(StartUpdate), e.Error);

SetSubItemText(task.Item.SubItems[1], "Error");
SetImageKey(task.Item, "status_error");
SetSubItemText(view.SubItems[1], "Error");
SetImageKey(view, "status_error");

return;
}

SetSubItemText(task.Item.SubItems[1], "Done");
SetImageKey(task.Item, "status_done");
SetSubItemText(view.SubItems[1], "Done");
SetImageKey(view, "status_done");

int left = amountToDownload.Decrement();

Expand All @@ -197,7 +199,7 @@ private void Task_TaskProgressChanged(object sender, ProgressChangedEventArgs e)
{
UpdatableTask task = (UpdatableTask)sender;

SetSubItemText(task.Item.SubItems[2], $"{e.ProgressPercentage}%");
SetSubItemText(m_items[task].SubItems[2], $"{e.ProgressPercentage}%");
}

public void CancelUpdate()
Expand Down Expand Up @@ -306,23 +308,26 @@ public void Rollback()
{
IsBusy = true;

foreach (UpdatableTask task in m_updateTasks)
foreach (var item in m_items)
{
UpdatableTask task = item.Key;
ListViewItem view = item.Value;

if (task.IsCancelled || (!task.IsCompleted && !task.IsRunning))
{
SetSubItemText(task.Item.SubItems[1], "No action");
SetSubItemText(view.SubItems[1], "No action");

SetImageKey(task.Item, "status_warning");
SetImageKey(view, "status_warning");

continue;
}


task.Cancel();

SetSubItemText(task.Item.SubItems[1], "Rolled back");
SetSubItemText(view.SubItems[1], "Rolled back");

SetImageKey(task.Item, "status_warning");
SetImageKey(view, "status_warning");
}

IsBusy = false;
Expand Down
3 changes: 3 additions & 0 deletions UpdateLib/UpdateLib/UpdateLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@
<Compile Include="Utils\ExtensionMethods.cs" />
<Compile Include="Utils\IOUtils.cs" />
<Compile Include="Utils\Lazy.cs" />
<Compile Include="Utils\NetworkUtils.cs" />
<Compile Include="Utils\RegistryHelper.cs" />
<Compile Include="Win32\NativeMethods.cs" />
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram1.cd" />
<None Include="Resources\status_working.png" />
<None Include="Resources\status_update.png" />
<None Include="Resources\status_warning.png" />
Expand Down
1 change: 1 addition & 0 deletions UpdateLib/UpdateLib/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ internal bool RestartApp(bool update = false, bool silent = false, bool waitForP

ProcessStartInfo startInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().Location, arguments);

startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.UseShellExecute = true;

if (asAdmin)
Expand Down
20 changes: 20 additions & 0 deletions UpdateLib/UpdateLib/Utils/NetworkUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using MatthiWare.UpdateLib.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatthiWare.UpdateLib.Utils
{
public static class NetworkUtils
{


public static bool HasConnection()
{
int desc;
return NativeMethods.InternetGetConnectedState(out desc, 0);
}

}
}
16 changes: 16 additions & 0 deletions UpdateLib/UpdateLib/Win32/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace MatthiWare.UpdateLib.Win32
{
internal static class NativeMethods
{

[DllImport("wininet.dll")]
internal static extern bool InternetGetConnectedState(out int connDescription, int reservedValue);

}
}

0 comments on commit ce828fe

Please sign in to comment.