ApplicationUpdateCheck.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using MediaBrowser.Model.Updates;
  2. using System;
  3. using System.Deployment.Application;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.ClickOnce
  7. {
  8. /// <summary>
  9. /// Class ApplicationUpdateCheck
  10. /// </summary>
  11. public class ApplicationUpdateCheck
  12. {
  13. /// <summary>
  14. /// The _task completion source
  15. /// </summary>
  16. private TaskCompletionSource<CheckForUpdateResult> _taskCompletionSource;
  17. /// <summary>
  18. /// The _progress
  19. /// </summary>
  20. private IProgress<double> _progress;
  21. /// <summary>
  22. /// Checks for application update.
  23. /// </summary>
  24. /// <param name="cancellationToken">The cancellation token.</param>
  25. /// <param name="progress">The progress.</param>
  26. /// <returns>Task{CheckForUpdateCompletedEventArgs}.</returns>
  27. /// <exception cref="System.InvalidOperationException">Current deployment is not a ClickOnce deployment</exception>
  28. public Task<CheckForUpdateResult> CheckForApplicationUpdate(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<CheckForUpdateResult>();
  36. var deployment = ApplicationDeployment.CurrentDeployment;
  37. cancellationToken.Register(deployment.CheckForUpdateAsyncCancel);
  38. cancellationToken.ThrowIfCancellationRequested();
  39. deployment.CheckForUpdateCompleted += deployment_CheckForUpdateCompleted;
  40. deployment.CheckForUpdateProgressChanged += deployment_CheckForUpdateProgressChanged;
  41. deployment.CheckForUpdateAsync();
  42. return _taskCompletionSource.Task;
  43. }
  44. /// <summary>
  45. /// To the result.
  46. /// </summary>
  47. /// <param name="args">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
  48. /// <returns>CheckForUpdateResult.</returns>
  49. private CheckForUpdateResult ToResult(CheckForUpdateCompletedEventArgs args)
  50. {
  51. return new CheckForUpdateResult
  52. {
  53. AvailableVersion = args.AvailableVersion,
  54. IsUpdateAvailable = args.UpdateAvailable
  55. };
  56. }
  57. /// <summary>
  58. /// Handles the CheckForUpdateCompleted event of the deployment control.
  59. /// </summary>
  60. /// <param name="sender">The source of the event.</param>
  61. /// <param name="e">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
  62. void deployment_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e)
  63. {
  64. var deployment = ApplicationDeployment.CurrentDeployment;
  65. deployment.CheckForUpdateCompleted -= deployment_CheckForUpdateCompleted;
  66. deployment.CheckForUpdateProgressChanged -= deployment_CheckForUpdateProgressChanged;
  67. if (e.Error != null)
  68. {
  69. _taskCompletionSource.SetException(e.Error);
  70. }
  71. else if (e.Cancelled)
  72. {
  73. _taskCompletionSource.SetCanceled();
  74. }
  75. else
  76. {
  77. _taskCompletionSource.SetResult(ToResult(e));
  78. }
  79. }
  80. /// <summary>
  81. /// Handles the CheckForUpdateProgressChanged event of the deployment control.
  82. /// </summary>
  83. /// <param name="sender">The source of the event.</param>
  84. /// <param name="e">The <see cref="DeploymentProgressChangedEventArgs" /> instance containing the event data.</param>
  85. void deployment_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
  86. {
  87. _progress.Report(e.ProgressPercentage);
  88. }
  89. }
  90. }