ApplicationUpdateCheck.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using MediaBrowser.Model.Tasks;
  2. using System;
  3. using System.Deployment.Application;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace MediaBrowser.Common.Updates
  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<CheckForUpdateCompletedEventArgs> _taskCompletionSource;
  17. /// <summary>
  18. /// The _progress
  19. /// </summary>
  20. private IProgress<TaskProgress> _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<CheckForUpdateCompletedEventArgs> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<TaskProgress> 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<CheckForUpdateCompletedEventArgs>();
  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. /// Handles the CheckForUpdateCompleted event of the deployment control.
  46. /// </summary>
  47. /// <param name="sender">The source of the event.</param>
  48. /// <param name="e">The <see cref="CheckForUpdateCompletedEventArgs" /> instance containing the event data.</param>
  49. void deployment_CheckForUpdateCompleted(object sender, CheckForUpdateCompletedEventArgs e)
  50. {
  51. var deployment = ApplicationDeployment.CurrentDeployment;
  52. deployment.CheckForUpdateCompleted -= deployment_CheckForUpdateCompleted;
  53. deployment.CheckForUpdateProgressChanged -= deployment_CheckForUpdateProgressChanged;
  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 CheckForUpdateProgressChanged 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_CheckForUpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
  73. {
  74. _progress.Report(new TaskProgress { PercentComplete = e.ProgressPercentage });
  75. }
  76. }
  77. }