Skip to content

Instantly share code, notes, and snippets.

@BaileyMillerSSI
Created January 2, 2020 18:44
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 BaileyMillerSSI/8aa4f429227510cdc68880c533cab8fd to your computer and use it in GitHub Desktop.
Save BaileyMillerSSI/8aa4f429227510cdc68880c533cab8fd to your computer and use it in GitHub Desktop.
class Program
{
static string OutputPath = @"C:\Users\baile\Downloads\Data";
static HttpClient Web = new HttpClient();
static async Task Main(string[] args)
{
var Codes = new List<int>();
for (int i = 0; i < 200; i++)
{
Codes.Add(i + 1);
}
Web.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
var taskList = new List<Task>();
Directory.CreateDirectory(OutputPath);
foreach (int rCode in Codes)
{
taskList.Add(Task.Run(async () =>
{
try
{
var _path = Path.Combine(OutputPath, $"{rCode}-{DateTime.Now.ToString("yyyy-MM-ddHHmmssfff")}.json");
using (Stream dataStream = await Web.GetStreamAsync($"todos/{rCode}"))
using (FileStream localStream = new FileStream(_path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
await dataStream.CopyToAsync(localStream);
}
}
catch (Exception er)
{
}
}));
}
//https://www.reddit.com/r/csharp/comments/ej1hpl/writing_to_files_inside_of_a_task_which_is/
Console.WriteLine("Starting Reddit File Concurrency Tests");
await Task.WhenAll(taskList.ToArray());
Console.WriteLine("Test Completed, press any key to exit");
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment