Wednesday, April 25, 2007

Asynchronous Programming Model - 2.0

I have been reading about the Asynchronous Programming Model in Jeffrey Rithcer's book CLR via C#. I have really enjoyed reading it and i thought of writing something about it.

The Asynchronous Programming Model (APM), as implemented by Delegates, consists of three parts:

- BeginInvoke,
- EndInvoke and
- Rendezvous techniques.

BeginInvoke starts an algorithm, impelmented via a method, on a new thread. EndInvoke retrieves the result of that method. The Rendezvous techniques allow you to determine when the asynchronous operation has completed.

There are three different types of Rendezvous techniques you can use to retrieve the results of an asynchronous delegate invocation

1. Wait Till Completion
2. Polling
3. Method Callback

Wait Till Completion
Wait-Till-Completion is implemented via EndInvoke. Calling this method will block the current thread until the results of the asynchronous method are available. This is the least effective method, because it eliminates all the benefits of APM.
private delegate string StringReturningDelegate();
private void Main()
{
// create an instance of the delegate pointing to a method that takes ten seconds to complete.
StringReturningDelegate fd = new StringReturningDelegate (MethodThatTakes10SecondsToComplete);
// Begin invocation of this delegate
IAsyncResult result = fd.BeginInvoke(null, null);
// Immediately call EndInvoke, which will block for, oh, say, right about ten seconds
string s = fd.EndInvoke(result);
Console.Write(s);
Console.Read();
}
// A method that takes 10 seconds, then returns a string private string.
MethodThatTakes10SecondsToComplete()
{
Thread.Sleep(10000);
return "Done!";
}
Polling
In this technique, you check a property of the IAsyncResult object called IsCompleted. This property will return false until the async operation has completed.

// a delegate for a method that takes no params and returns a string.
private delegate string StringReturningDelegate();
private void Main()
{
// create an instance of the delegate pointing to a method that takes ten seconds to complete
StringReturningDelegate fd = new StringReturningDelegate (MethodThatTakes10SecondsToComplete);
// Begin invocation of this delegate
IAsyncResult receipt = fd.BeginInvoke(null, null);
Console.Write("Working");
// Poll IsCompleted until it returns true; Sleep the current thread between checks to reduce CPU usage
while (!receipt.IsCompleted)
{
Thread.Sleep(500);
// wait half a sec
Console.Write('.');
}
string result = fd.EndInvoke(receipt);
Console.Write(result);
Console.Read();
}
// A method that takes 10 seconds, then returns a string
private string MethodThatTakes10SecondsToComplete()
{
Thread.Sleep(10000);
return "Done!";
}
Method Callback
In this technique, you pass a delegate to the BeginInvoke method that will be called when the asynchronous operation has completed. It will not block your execution, or waste any CPU cycles. This is the most effective emthod of Rendezvous.
//a delegate for a method that takes no params and returns a string.
private delegate string StringReturningDelegate();
private void Main()
{
// create an instance of the delegate pointing to a method that takes ten seconds to complete.
StringReturningDelegate fd = new StringReturningDelegate (MethodThatTakes10SecondsToComplete);
// Begin invocation of this delegate
fd.BeginInvoke(AsyncOpComplete, null);
// Do tons of work here. No, seriously.
Console.Read();
}
/// /// Retrieves the results of MethodThatTakes10SecondsToComplete when called asynchronously/// ///
The IAsyncResult receipt.
private void AsyncOpComplete(IAsyncResult receipt)
{
// Cast to the actual object so that we can access the delegate
AsyncResult result = (AsyncResult)receipt;
// retrieve the calling delegate
StringReturningDelegate gsld = (StringReturningDelegate)result.AsyncDelegate;
// Retrieve our results; this is guaranteed not to block, as the async op is complete
string result = gsld.EndInvoke(receipt);
//write the result to the console
Console.Write(result);
}
// A method that takes 10 seconds, then returns a string
private string MethodThatTakes10SecondsToComplete()
{
Thread.Sleep(10000);
return "Done!";
}

Scientists break internet speed record

A group of researchers in the Internet2 consortium has set a new record for sending data through the Internet at more than nine gigabits per second.The team broke the old record of 7.67 Gbps, which was set last December. Using modified protocols, they were able to send data across a 20,000 mile path at a constant rate of 9.08 Gbps.At this speed, a high definition version of a movie could be downloaded in just a few seconds, instead of over 40 hours on a typical broadband connection.....

Tuesday, April 24, 2007

Google vs. Microsoft: reality check

I know google has been doing great work over the past year or two, but is it that Microsoft is out? I odnt think so. I came across Jobsblog and read quite a few intersting things. I have collected a few of those facts and here they are:

Why Google IS Afraid of Microsoft, Big Time

HIGHLIGHTS FROM THE ARTICLE:
Microsoft pummels Google in the “In-Game Advertsing” space. How? Google buys a small San-Fran company to meet the challenge. Microsoft buys the world-leader in the industry for In-game advertising.
Microsoft delivers a solid uppercut in Voice-activated local directory assistance. How? Google announces an experimental service that may not be available at all times and may not work for all users. (Cute) Microsoft acquires TellMe Networks. Heard of them? Most likely, you’ve been using them for the longest time. Almost half of all directory assistance calls are processed on TellMe’s voice platform, and roughly one in three Americans use Tellme every year to get things done.

Assertion that Microsoft's 'Dead' Doesn't Compute
QUOTES FROM THE ARTICLE:
“When a software runs more than 90% of the desktops on the planet — and will for the foreseeable future — it's simply not dead.”“Windows runs on the vast majority of desktops in the world; Linux and OSX make up less than 10% combined”(Microsoft) “…earned $12.6 billion after taxes in its last fiscal year.

Well this article was written by a guy who currently works for Microsoft (you might have guessed that) and is an ex-googler