From 767cdc1f6f6a63ce997fc9476911e2c361f9d402 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Wed, 20 Feb 2013 20:33:05 -0500 Subject: Pushing missing changes --- .../Updates/ApplicationUpdateCheck.cs | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs (limited to 'MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs') diff --git a/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs b/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs new file mode 100644 index 0000000000..7501d7321b --- /dev/null +++ b/MediaBrowser.Common/Updates/ApplicationUpdateCheck.cs @@ -0,0 +1,92 @@ +using MediaBrowser.Model.Tasks; +using System; +using System.Deployment.Application; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Updates +{ + /// + /// Class ApplicationUpdateCheck + /// + public class ApplicationUpdateCheck + { + /// + /// The _task completion source + /// + private TaskCompletionSource _taskCompletionSource; + + /// + /// The _progress + /// + private IProgress _progress; + + /// + /// Checks for application update. + /// + /// The cancellation token. + /// The progress. + /// Task{CheckForUpdateCompletedEventArgs}. + /// Current deployment is not a ClickOnce deployment + public Task CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress progress) + { + if (!ApplicationDeployment.IsNetworkDeployed) + { + throw new InvalidOperationException("Current deployment is not network deployed."); + } + + _progress = progress; + + _taskCompletionSource = new TaskCompletionSource(); + + var deployment = ApplicationDeployment.CurrentDeployment; + + cancellationToken.Register(deployment.CheckForUpdateAsyncCancel); + + cancellationToken.ThrowIfCancellationRequested(); + + deployment.CheckForUpdateCompleted += deployment_CheckForUpdateCompleted; + deployment.CheckForUpdateProgressChanged += deployment_CheckForUpdateProgressChanged; + + deployment.CheckForUpdateAsync(); + + return _taskCompletionSource.Task; + } + + /// + /// Handles the CheckForUpdateCompleted event of the deployment control. + /// + /// The source of the event. + /// The instance containing the event data. + void deployment_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e) + { + var deployment = ApplicationDeployment.CurrentDeployment; + + deployment.CheckForUpdateCompleted -= deployment_CheckForUpdateCompleted; + deployment.CheckForUpdateProgressChanged -= deployment_CheckForUpdateProgressChanged; + + if (e.Error != null) + { + _taskCompletionSource.SetException(e.Error); + } + else if (e.Cancelled) + { + _taskCompletionSource.SetCanceled(); + } + else + { + _taskCompletionSource.SetResult(e); + } + } + + /// + /// Handles the CheckForUpdateProgressChanged event of the deployment control. + /// + /// The source of the event. + /// The instance containing the event data. + void deployment_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) + { + _progress.Report(new TaskProgress { PercentComplete = e.ProgressPercentage }); + } + } +} -- cgit v1.2.3