Created
January 2, 2020 18:44
-
-
Save BaileyMillerSSI/8aa4f429227510cdc68880c533cab8fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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