static void Main(string[] args) { var subtask = new SnSubtask("Thinking hard", "Pretending to do something."); subtask.Start(); var count = new Random().Next(10, 15); for (var i = 0; i < count; i++) { Thread.Sleep(1000); subtask.Progress(i, count, i, count, $"Still pretending: {i}..."); } subtask.Finish("Cannot pretend anymore."); }
// ================================================================================================== Preview generation private static async Task GenerateImagesAsync(CancellationToken cancellationToken) { int previewsFolderId; string contentPath; var downloadingSubtask = new SnSubtask("Downloading", "Downloading file and other information"); downloadingSubtask.Start(); try { var fileInfo = await GetFileInfoAsync().ConfigureAwait(false); if (fileInfo == null) { Logger.WriteWarning(ContentId, 0, "Content not found."); downloadingSubtask.Finish(); return; } cancellationToken.ThrowIfCancellationRequested(); previewsFolderId = await GetPreviewsFolderIdAsync(); if (previewsFolderId < 1) { Logger.WriteWarning(ContentId, 0, "Previews folder not found, maybe the content is missing."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(10, 100, 2, 110, "File info downloaded."); cancellationToken.ThrowIfCancellationRequested(); contentPath = fileInfo.Path; if (Config.ImageGeneration.CheckLicense) { CheckLicense(contentPath.Substring(contentPath.LastIndexOf('/') + 1)); } } catch (Exception ex) { Logger.WriteError(ContentId, message: "Error during initialization. The process will exit without generating images.", ex: ex, startIndex: StartIndex, version: Version); return; } #region For tests //if (contentPath.EndsWith("freeze.txt", StringComparison.OrdinalIgnoreCase)) // Freeze(); //if (contentPath.EndsWith("overflow.txt", StringComparison.OrdinalIgnoreCase)) // Overflow(); #endregion await using var docStream = await GetBinaryAsync(); if (docStream == null) { Logger.WriteWarning(ContentId, 0, $"Document not found; maybe the content or its version {Version} is missing."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(100, 100, 10, 110, "File downloaded."); cancellationToken.ThrowIfCancellationRequested(); if (docStream.Length == 0) { await SetPreviewStatusAsync(0); // PreviewStatus.EmptyDocument downloadingSubtask.Finish(); return; } downloadingSubtask.Finish(); _generatingPreviewSubtask = new SnSubtask("Generating images"); _generatingPreviewSubtask.Start(); var extension = contentPath.Substring(contentPath.LastIndexOf('.')); await PreviewImageGenerator.GeneratePreviewAsync(extension, docStream, new PreviewGenerationContext( ContentId, previewsFolderId, StartIndex, MaxPreviewCount, Config.ImageGeneration.PreviewResolution, Version), cancellationToken).ConfigureAwait(false); _generatingPreviewSubtask.Finish(); }
// ================================================================================================== Preview generation protected static void GenerateImages() { int previewsFolderId; string contentPath; var downloadingSubtask = new SnSubtask("Downloading", "Downloading file and other information"); downloadingSubtask.Start(); try { previewsFolderId = GetPreviewsFolderId(); if (previewsFolderId < 1) { Logger.WriteWarning(ContentId, 0, "Previews folder not found, maybe the content is missing."); downloadingSubtask.Finish(); return; } var fileInfo = GetFileInfo(); if (fileInfo == null) { Logger.WriteWarning(ContentId, 0, "Content not found."); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(10, 100, 2, 110, "File info downloaded."); contentPath = fileInfo["Path"].Value <string>(); CheckLicense(contentPath.Substring(contentPath.LastIndexOf('/') + 1)); } catch (Exception ex) { Logger.WriteError(ContentId, message: "Error during initialization. The process will exit without generating images.", ex: ex, startIndex: StartIndex, version: Version); return; } //if (contentPath.EndsWith("freeze.txt", StringComparison.OrdinalIgnoreCase)) // Freeze(); //if (contentPath.EndsWith("overflow.txt", StringComparison.OrdinalIgnoreCase)) // Overflow(); using (var docStream = GetBinary()) { if (docStream == null) { Logger.WriteWarning(ContentId, 0, string.Format("Document not found; maybe the content or its version {0} is missing.", Version)); downloadingSubtask.Finish(); return; } downloadingSubtask.Progress(100, 100, 10, 110, "File downloaded."); if (docStream.Length == 0) { SetPreviewStatus(0); // PreviewStatus.EmptyDocument downloadingSubtask.Finish(); return; } downloadingSubtask.Finish(); _generatingPreviewSubtask = new SnSubtask("Generating images"); _generatingPreviewSubtask.Start(); var extension = contentPath.Substring(contentPath.LastIndexOf('.')); PreviewImageGenerator.GeneratePreview(extension, docStream, new PreviewGenerationContext( ContentId, previewsFolderId, StartIndex, MaxPreviewCount, Configuration.PreviewResolution, Version)); _generatingPreviewSubtask.Finish(); } }