public
Last active

Node.NET: Non blocking API .NET

  • Download Gist
non_blocking_api_dot_net.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
using System;
using System.IO;
using System.Net;
using System.Security.AccessControl;
using System.Text;
 
namespace SimpleExample
{
class Program
{
private const int BUFFER_SIZE = 10000;
 
static void Main(string[] args)
{
// So we need to read file in non blocking way,
// request some stuff from google and exit..
 
var file = new FileStream("Program.cs", FileMode.Open, FileAccess.Read,
FileShare.None, BUFFER_SIZE, true);
 
var buffer = new byte[BUFFER_SIZE];
 
file.BeginRead(buffer, 0, BUFFER_SIZE,
r =>
{
var read = file.EndRead(r);
 
var stuff = Encoding.Default.GetString(buffer, 0, read);
 
Console.WriteLine(stuff);
file.Close();
 
// Lets go futher... lets load some stuff from web
var url = "http://www.google.com/search?q=node.js";
var req = WebRequest.Create(url);
req.BeginGetResponse(
r2 =>
{
var resp = req.EndGetResponse(r2);
Console.WriteLine("{0} headers recieved.",
resp.Headers.Count);
}, null);
}, null);
 
Console.ReadLine();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.