Tasks with Timeouts – Contd.

As mentioned in the last post we can now have tasks with individual timeouts. The code looked a little heavy. Can we do better ? Yes we can !!


var tasks = new List<Task>();
try
{
//instead of checking for fault within the continutaion, 
//we can just use a TaskContinuationOption to tell communicate the right semantics

var t1 = Task.Factory.StartNew(_ => LongRunningTask(), TaskCreationOptions.AttachedToParent)
              .TimeoutAfter(1000)
              .ContinueWith(t => SomethingUsefulWithTheResult(), 
                                 TaskContinuationOptions.NotOnFaulted);

var t2 = Task.Factory.StartNew(_ => LongRunningTask(), TaskCreationOptions.AttachedToParent)
              .ContinueWith(t => SomethingUsefulWithTheResult());

var t3 = Task.Factory.StartNew(_ => LongRunningTask("Entering task3"), TaskCreationOptions.AttachedToParent)
              .ContinueWith(t => SomethingUsefulWithTheResult());
            
tasks.Add(t1);
tasks.Add(t2);
tasks.Add(t3);
                            
Task.WaitAll(tasks.ToArray());
}
catch (Exception ex)
{                
    Console.WriteLine("There was an exception");
    Console.WriteLine(ex.InnerException.Message);               
}

Freebase : Level 3

It has been a long time since I have written a post, I am a little nervous now. Freebase has moved into a different league with Google acquiring it in 2010(Right about the time when I used Freebase to do the exact same thing that Google does now, not gloating just saying search engines just weren’t doing enough besides Page-ranking).
More information can be found on the following two links
Google Acquires Metaweb
Semantic Search

The results are evident now , search for a movie and you will see a nifty bar on the side showing a whole lot more information, information that was always there just not being used enough. Enough of talk, back to code.
I got some feedback on the small class that I wrote to query Freebase. It was OK, but cumbersome to use. So, there you go it is all dynamic now.

                dynamic thepolice = new ExpandoObject();
                thepolice.type = "/music/artist";
                thepolice.name = "The Police";
                thepolice.album = new Dictionary<Object, Object>();
                thepolice.album.Add("name", null);
                thepolice.album.Add("limit", 2);
                thepolice.album.Add("genre", new Object[5]);

I agree with .net4 and beyond this is the way developers would like to use it. I would definitely like to use it this way. This time the code is a lot cleaner(I am still learning, I am not evil by default). There is a class that I borrowed from MSDN that allows you to switch over types(more useful than you think). Code is now smaller, with a few tests as well. The entire code is available on Github Freebase.net.