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
// Excerpt from https://blogs.msdn.microsoft.com/seteplia/2018/09/05/combining-iterator-blocks-and-async-methods-in-c/ | |
public static IEnumerable<Task<int>> ParseFile(string path) | |
{ | |
if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } | |
// OpenWithRetryPolicyAsync uses RetryPolicy to try to open the file more than once. | |
// The method throws FileNotFoundException if the file does not exists. | |
using (var file = OpenWithRetryPolicyAsync(path).Result) | |
{ |
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
public static IEnumerable<Task<int>> ParseFile(string path) |
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
Task<int> firstTask = ParseFile(path).First(); |
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
// Never do this! | |
Task<int> firstTask = ParseFile(path).Skip(1).First(); |
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
// This doesn't work. | |
public static Func<string> GetContentGetter(string configPath) | |
{ | |
using (var reader = new StreamReader(configPath)) | |
{ | |
return () => reader.ReadToEnd(); | |
} | |
} |
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
// This doesn't work. | |
public static Task<string> GetContentAsync(string configPath) | |
{ | |
using (var reader = new StreamReader(configPath)) | |
{ | |
return reader.ReadToEndAsync(); | |
} | |
} |
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
public static async Task<string> GetContentAsync(string configPath) | |
{ | |
using (var reader = new StreamReader(configPath)) | |
{ | |
return await reader.ReadToEndAsync(); | |
} | |
} |
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
// This won't work | |
public static IEnumerable<Task<int>> ParseFile(string path) | |
{ | |
using (var reader = new StreamReader(path)) | |
{ | |
while (!reader.EndOfStream) | |
{ | |
yield return ReadAndParseAsync(reader); | |
} | |
} |
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
public static IEnumerable<Task<int>> ParseFile(string path) | |
{ | |
using (var reader = new StreamReader(path)) | |
{ | |
while (!reader.EndOfStream) | |
{ | |
Task<int> readLineTask = null; | |
try | |
{ | |
readLineTask = ReadAndParseAsync(reader); |
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
public static bool Read(int[] buffer, out int bytesRead) |
OlderNewer