ApplicationUpdater.cs 3.3 KB

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