ApplicationUpdater.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.ComponentModel;
  3. using System.Deployment.Application;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.ClickOnce
  7. {
  8. /// <summary>
  9. /// Class ApplicationUpdater
  10. /// </summary>
  11. public class ApplicationUpdater
  12. {
  13. /// <summary>
  14. /// The _task completion source
  15. /// </summary>
  16. private TaskCompletionSource<AsyncCompletedEventArgs> _taskCompletionSource;
  17. /// <summary>
  18. /// The _progress
  19. /// </summary>
  20. private IProgress<double> _progress;
  21. /// <summary>
  22. /// Updates the application
  23. /// </summary>
  24. /// <param name="cancellationToken">The cancellation token.</param>
  25. /// <param name="progress">The progress.</param>
  26. /// <returns>Task{AsyncCompletedEventArgs}.</returns>
  27. /// <exception cref="System.InvalidOperationException">Current deployment is not network deployed.</exception>
  28. public Task<AsyncCompletedEventArgs> UpdateApplication(CancellationToken cancellationToken, IProgress<double> progress)
  29. {
  30. if (!ApplicationDeployment.IsNetworkDeployed)
  31. {
  32. throw new InvalidOperationException("Current deployment is not network deployed.");
  33. }
  34. _progress = progress;
  35. _taskCompletionSource = new TaskCompletionSource<AsyncCompletedEventArgs>();
  36. var deployment = ApplicationDeployment.CurrentDeployment;
  37. cancellationToken.Register(deployment.UpdateAsyncCancel);
  38. cancellationToken.ThrowIfCancellationRequested();
  39. deployment.UpdateCompleted += deployment_UpdateCompleted;
  40. deployment.UpdateProgressChanged += deployment_UpdateProgressChanged;
  41. deployment.UpdateAsync();
  42. return _taskCompletionSource.Task;
  43. }
  44. /// <summary>
  45. /// Handles the UpdateCompleted event of the deployment control.
  46. /// </summary>
  47. /// <param name="sender">The source of the event.</param>
  48. /// <param name="e">The <see cref="AsyncCompletedEventArgs" /> instance containing the event data.</param>
  49. void deployment_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
  50. {
  51. var deployment = ApplicationDeployment.CurrentDeployment;
  52. deployment.UpdateCompleted -= deployment_UpdateCompleted;
  53. deployment.UpdateProgressChanged -= deployment_UpdateProgressChanged;
  54. if (e.Error != null)
  55. {
  56. _taskCompletionSource.SetException(e.Error);
  57. }
  58. else if (e.Cancelled)
  59. {
  60. _taskCompletionSource.SetCanceled();
  61. }
  62. else
  63. {
  64. _taskCompletionSource.SetResult(e);
  65. }
  66. }
  67. /// <summary>
  68. /// Handles the UpdateProgressChanged event of the deployment control.
  69. /// </summary>
  70. /// <param name="sender">The source of the event.</param>
  71. /// <param name="e">The <see cref="DeploymentProgressChangedEventArgs" /> instance containing the event data.</param>
  72. void deployment_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
  73. {
  74. _progress.Report(e.ProgressPercentage);
  75. }
  76. }
  77. }