Last active
March 9, 2016 06:44
-
-
Save kazuk/20947c12764647670ecb 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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Task.Run(MainAsync).Wait() ; | |
} | |
private static async Task MainAsync() | |
{ | |
var stream = File.OpenWrite("test6.txt"); | |
List<Task> tasks = new List<Task>(); | |
Random rnd = new Random(); | |
foreach (var value in Enumerable.Range(1,100)) | |
{ | |
var delay = rnd.Next(1000); | |
tasks.Add( WriteInt(value,stream,delay) ); | |
} | |
Task.WaitAll(tasks.ToArray()); | |
stream.Close(); | |
} | |
private static async Task WriteInt(int value, FileStream stream, int delay) | |
{ | |
await Task.Delay(delay).ConfigureAwait(false); | |
var text = $"WriteInt: {value} {Environment.NewLine}"; | |
var bytes = Encoding.ASCII.GetBytes(text); | |
await stream.WriteAsync(bytes, 0, bytes.Length); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment