From 868a7ce9c8b50bd64c8b5ae33fec77abfd25ef7c Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Thu, 21 Feb 2013 23:23:06 -0500 Subject: isolated clickonce dependancies --- MediaBrowser.ClickOnce/ApplicationUpdater.cs | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 MediaBrowser.ClickOnce/ApplicationUpdater.cs (limited to 'MediaBrowser.ClickOnce/ApplicationUpdater.cs') diff --git a/MediaBrowser.ClickOnce/ApplicationUpdater.cs b/MediaBrowser.ClickOnce/ApplicationUpdater.cs new file mode 100644 index 0000000000..29af406f1e --- /dev/null +++ b/MediaBrowser.ClickOnce/ApplicationUpdater.cs @@ -0,0 +1,92 @@ +using System; +using System.ComponentModel; +using System.Deployment.Application; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.ClickOnce +{ + /// + /// Class ApplicationUpdater + /// + public class ApplicationUpdater + { + /// + /// The _task completion source + /// + private TaskCompletionSource _taskCompletionSource; + + /// + /// The _progress + /// + private IProgress _progress; + + /// + /// Updates the application + /// + /// The cancellation token. + /// The progress. + /// Task{AsyncCompletedEventArgs}. + /// Current deployment is not network deployed. + public Task UpdateApplication(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.UpdateAsyncCancel); + + cancellationToken.ThrowIfCancellationRequested(); + + deployment.UpdateCompleted += deployment_UpdateCompleted; + deployment.UpdateProgressChanged += deployment_UpdateProgressChanged; + + deployment.UpdateAsync(); + + return _taskCompletionSource.Task; + } + + /// + /// Handles the UpdateCompleted event of the deployment control. + /// + /// The source of the event. + /// The instance containing the event data. + void deployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e) + { + var deployment = ApplicationDeployment.CurrentDeployment; + + deployment.UpdateCompleted -= deployment_UpdateCompleted; + deployment.UpdateProgressChanged -= deployment_UpdateProgressChanged; + + if (e.Error != null) + { + _taskCompletionSource.SetException(e.Error); + } + else if (e.Cancelled) + { + _taskCompletionSource.SetCanceled(); + } + else + { + _taskCompletionSource.SetResult(e); + } + } + + /// + /// Handles the UpdateProgressChanged event of the deployment control. + /// + /// The source of the event. + /// The instance containing the event data. + void deployment_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e) + { + _progress.Report(e.ProgressPercentage); + } + } +} -- cgit v1.2.3