コード例 #1
0
        public void Start()
        {
            if (!CanStart)
            {
                return;
            }

            IsActive     = true;
            IsSuccessful = false;
            IsCanceled   = false;
            IsFailed     = false;

            Task.Run(async() =>
            {
                // Create cancellation token source
                _cancellationTokenSource = new CancellationTokenSource();

                // Create progress operation
                ProgressOperation = ProgressManager.CreateOperation();

                try
                {
                    // If download option is not set - get the best download option
                    if (DownloadOption == null)
                    {
                        DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format);
                    }

                    await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token);

                    if (_settingsService.ShouldInjectTags)
                    {
                        await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token);
                    }

                    if (SubtitleOption != null && SubtitleOption.ClosedCaptionTrackInfos.Any())
                    {
                        await _downloadService.DownloadSubtitleAsync(SubtitleOption, FilePath);
                    }

                    IsSuccessful = true;
                }
                catch (OperationCanceledException)
                {
                    IsCanceled = true;
                }
                catch (Exception ex)
                {
                    IsFailed   = true;
                    FailReason = ex.Message;
                }
                finally
                {
                    IsActive = false;

                    _cancellationTokenSource.Dispose();
                    ProgressOperation.Dispose();
                }
            });
        }