Skip to content

Instantly share code, notes, and snippets.

@ljw1004
Created July 21, 2016 17:53
Show Gist options
  • Save ljw1004/c3029735052e5ffd2303fee662758585 to your computer and use it in GitHub Desktop.
Save ljw1004/c3029735052e5ffd2303fee662758585 to your computer and use it in GitHub Desktop.
Simple example of async ValueTask
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static void Main()
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
using (var reader = new Reader(new FileStream("ConsoleApplicationCS.exe", FileMode.Open, FileAccess.Read)))
{
long sum = 0;
while (true)
{
var b = await reader.GetNextByteAsync();
if (!b.HasValue) break;
sum += b.Value;
}
Console.WriteLine($"Sum of bytes: {sum}");
}
}
}
class Reader : IDisposable
{
byte[] buf = new byte[1024];
int pos = 0, len=0;
Stream stream;
public async ValueTask<byte?> GetNextByteAsync()
{
// If we've exhausted the current buffer, fetch another buffer
if (pos >= len)
{
len = await stream.ReadAsync(buf, 0, buf.Length);
pos = 0;
}
// If there's anything left in the current buffer, return it
if (pos < len)
{
return buf[pos++];
}
// Otherwise signal EOF
return null;
}
public Reader(Stream stream)
{
this.stream = stream;
}
public void Dispose()
{
stream.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment