Skip to content

Instantly share code, notes, and snippets.

@kazuk
Last active March 9, 2016 06:44
Show Gist options
  • Save kazuk/20947c12764647670ecb to your computer and use it in GitHub Desktop.
Save kazuk/20947c12764647670ecb to your computer and use it in GitHub Desktop.
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