Skip to content

Instantly share code, notes, and snippets.

@milovidov983
Last active December 22, 2019 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milovidov983/e305890bfd8fbff29eb30dc0e75f9460 to your computer and use it in GitHub Desktop.
Save milovidov983/e305890bfd8fbff29eb30dc0e75f9460 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace v3 {
class Program {
static async Task Main(string[] args) {
var w = new Worker();
var links = await w.Start();
Console.WriteLine("Uploaded to cloud files URLs:");
foreach(var linkItem in links) {
Console.WriteLine(linkItem);
}
}
}
class Worker {
public async Task<IEnumerable<Uploaded>> Start() {
await Task.Delay(0);
var proc = new ImageProcessor();
var resizedImages = proc.GetResizedImages("sourceFile.jpg");
var tasks = new List<Task<Uploaded>>();
foreach(var image in resizedImages) {
var uploadTask = Task.Run(async () => await proc.UploadImage(image));
tasks.Add(uploadTask);
}
await Task.WhenAll(tasks);
return tasks.Select(x => x.Result);
}
}
class ImageProcessor {
public IEnumerable<Resized> GetResizedImages(string fileName) {
for (var i = 0; i < 10; i++) {
Console.WriteLine($"Start resize image {i}");
/// Имитируем работу конвертора изображений
var rand = new Random(DateTime.UtcNow.Millisecond);
Task.Delay(rand.Next(100, 400)).Wait();
yield return new Resized(i);
}
yield break;
}
public async Task<Uploaded> UploadImage(Resized image) {
Console.WriteLine($"Start upload image {image.Index}");
/// Имитируем работу загрузчика на облако
var rand = new Random(DateTime.UtcNow.Millisecond);
await Task.Delay(rand.Next(400, 700));
return new Uploaded(image.Index);
}
}
class Resized {
readonly int stream;
public Resized(int index) {
this.stream = index;
}
public int Index { get => stream; }
public override string ToString() {
return $"{nameof(Resized)} file - STREAM: {stream}";
}
}
class Uploaded {
readonly int url;
public Uploaded(int index) {
this.url = index;
}
public int Index { get => url; }
public override string ToString() {
return $"{nameof(Uploaded)} file - URL: {url}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment