Skip to content

Instantly share code, notes, and snippets.

@chaliy
Created July 27, 2010 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaliy/492333 to your computer and use it in GitHub Desktop.
Save chaliy/492333 to your computer and use it in GitHub Desktop.
Node.NET: Non blocking API .NET
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment