Skip to content

Commit

Permalink
#36 - Unknown version dialog and exit
Browse files Browse the repository at this point in the history
Switched frmo thread starts to TPL tasks with fault processing that passes the event back to the top. If it's a version exception we tell the user nicely and then exit
  • Loading branch information
tarwn committed Feb 16, 2013
1 parent b92871c commit 0f22ce0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
5 changes: 5 additions & 0 deletions Chocolatey Explorer/Chocolatey Explorer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chocolatey.Explorer.Test",
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Setup_ChocolateyGUI", "..\Setup_ChocolateyGUI\Setup_ChocolateyGUI.wixproj", "{1772C1E5-8896-4E28-933A-9413EB588A82}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0256ACD9-FC12-4C32-9795-5104410FAED0}"
ProjectSection(SolutionItems) = preProject
.nuget\packages.config = .nuget\packages.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class CachedPackagesService : IPackagesService, ICacheable
{
private static readonly ILog log = LogManager.GetLogger(typeof(CachedPackagesService));

public delegate void FinishedDelegate(IList<Package> packages);
public event PackagesService.FinishedDelegate RunFinshed;
public event PackagesService.FailedDelegate RunFailed;

private readonly IPackagesService packagesService;
private IList<Package> availablePackageCache;
Expand All @@ -22,6 +22,7 @@ class CachedPackagesService : IPackagesService, ICacheable
public CachedPackagesService()
{
packagesService = new ODataPackagesService();
packagesService.RunFailed += PackagesServiceRunFailed;
}

public void InvalidateCache()
Expand Down Expand Up @@ -95,5 +96,11 @@ public void OnUncachedInstalledRunFinished(IList<Package> packages)
packagesService.RunFinshed -= OnUncachedInstalledRunFinished;
OnRunFinshed(packages);
}

private void PackagesServiceRunFailed(System.Exception exc)
{
if (RunFailed != null)
RunFailed(exc);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Chocolatey.Explorer.Services
public interface IPackagesService
{
event PackagesService.FinishedDelegate RunFinshed;
event PackagesService.FailedDelegate RunFailed;
void ListOfPackages();
void ListOfInstalledPackages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Xml;
using Chocolatey.Explorer.Model;
using log4net;
using System.Threading.Tasks;

namespace Chocolatey.Explorer.Services
{
Expand All @@ -17,8 +18,8 @@ public class ODataPackagesService : IPackagesService
private readonly IPackageVersionXMLParser _xmlParser;
private readonly ChocolateyLibDirHelper _libDirHelper;

public delegate void PackagesServiceFinishedDelegate(IList<Package> packages);
public event PackagesService.FinishedDelegate RunFinshed;
public event PackagesService.FailedDelegate RunFailed;

public ODataPackagesService(): this(new SourceService(), new PackageVersionXMLParser())
{
Expand Down Expand Up @@ -107,17 +108,18 @@ private XmlDocument LoadFeedDoc(int skip)
return null;
}

public void ListOfInstalledPackages()
{
log.Info("Getting list of installed packages");
var thread = new Thread(ListOfInstalledPackagsThread) {IsBackground = true};
thread.Start();
}

private void ListOfInstalledPackagsThread()
{
OnRunFinshed(_libDirHelper.ReloadFromDir());
}
public void ListOfInstalledPackages()
{
log.Info("Getting list of installed packages");
Task.Factory.StartNew(() => _libDirHelper.ReloadFromDir())
.ContinueWith((task) =>
{
if (!task.IsFaulted)
OnRunFinshed(task.Result);
else if (task.IsFaulted && RunFailed != null)
RunFailed(task.Exception);
});
}

private void OnRunFinshed(IList<Package> packages)
{
Expand Down
19 changes: 12 additions & 7 deletions Chocolatey Explorer/Services/PackagesService/PackagesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Chocolatey.Explorer.Model;
using Chocolatey.Explorer.Powershell;
using log4net;
using System;
using System.Threading.Tasks;

namespace Chocolatey.Explorer.Services
{
Expand All @@ -19,6 +21,9 @@ public class PackagesService : IPackagesService
public delegate void FinishedDelegate(IList<Package> packages);
public event FinishedDelegate RunFinshed;

public delegate void FailedDelegate(Exception exc);
public event FailedDelegate RunFailed;

public PackagesService(): this(new RunAsync(), new SourceService())
{
}
Expand All @@ -42,13 +47,13 @@ public void ListOfPackages()
public void ListOfInstalledPackages()
{
log.Info("Getting list of installed packages");
var thread = new Thread(ListOfInstalledPackagsThread) {IsBackground = true};
thread.Start();
}

private void ListOfInstalledPackagsThread()
{
OnRunFinshed(_libDirHelper.ReloadFromDir());
Task.Factory.StartNew(() => _libDirHelper.ReloadFromDir())
.ContinueWith((task) => {
if (!task.IsFaulted)
OnRunFinshed(task.Result);
else if (task.IsFaulted && RunFailed != null)
RunFailed(task.Exception);
});
}

private void OutputChanged(string line)
Expand Down
15 changes: 14 additions & 1 deletion Chocolatey Explorer/View/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public PackageManager(IPackagesService packagesService, IPackageVersionService p
_packagesService.RunFinshed += PackagesServiceRunFinished;
_packageService.LineChanged += PackageServiceLineChanged;
_packageService.RunFinshed += PackageServiceRunFinished;
_packagesService.RunFailed += PackagesService_RunFailed;
ClearStatus();
PackageGrid.Focus();
UpdateInstallUninstallButtonLabel();
Expand Down Expand Up @@ -103,6 +104,18 @@ private void PackagesServiceRunFinished(IList<Package> packages)
}
}

private void PackagesService_RunFailed(Exception exc)
{
//TODO - should we do something to prevent them from using more of the app nd getting more errors?
if (exc is ChocolateyVersionUnknownException || (exc is AggregateException || exc.InnerException is IChocolateyService))
{
var result = MessageBox.Show("Chocolatey version could not be detected. Either Chocolatey is not installed or we cannot access it.", "Chocolatey not found");
Application.Exit();
}
else
MessageBox.Show(String.Format("An unexpected error occurred (good thing you're technical, eh?)\n{0}: {1}", exc.GetType().Name, exc.Message), "Unexpected Application Error");
}

private void VersionChangedHandler(PackageVersion version)
{
if (this.InvokeRequired)
Expand Down Expand Up @@ -264,7 +277,7 @@ private void QueryInstalledPackages()
packageTabControl.SelectedTab = tabInstalled;
lblProgressbar.Style = ProgressBarStyle.Marquee;
PackageGrid.DataSource = new List<Package>();
_packagesService.ListOfInstalledPackages();
_packagesService.ListOfInstalledPackages();
}
}

Expand Down

0 comments on commit 0f22ce0

Please sign in to comment.