Welcome to the CodeSmith Community!

CodeSmith Generator 6: Task-based Asynchronous Pattern

CodeSmith Community

A description has not yet been added to this group.

CodeSmith Generator 6: Task-based Asynchronous Pattern

Rate This
  • Comments 3

Overview

In CodeSmith Generator 6 we are upgrading the codebase to use .NET 4.0 framework. Using the.NET 4.0 framework gives us access the Task Parallel Library (TPL).  We plan to make heave use of the Task-based Asynchronous Pattern (TAP) to provide asynchronous call support to our API for generating templates. Microsoft describes the pattern as follows. “The Task-based Asynchronous Pattern (TAP) is a new pattern for asynchrony in the .NET Framework.  It is based on the Task and Task<TResult> types in the System.Threading.Tasks namespace, which are used to represent arbitrary asynchronous operations.” Much greater support will be added for this pattern in future version of the .NET framework, see Microsoft Visual Studio Async CTP.  However, there is no reason we can’t make use of the pattern today. 

Sample

First, a big warning that this syntax is subject to change as we evolve the code.  Here is an example of how you can use TAP and Generator API to compile and generate a template.

Task<CompileTemplateResult> task = TemplateEngine.CompileFileAsync(path);
task.ContinueWith(t =>
    {
        if (t.IsFaulted)
            return LogError(t.Exception);
        if (t.IsCanceled)
            return LogCanceled();

        CompileTemplateResult result = t.Result;
        CodeTemplate template = result.CreateTemplateInstance();

        return template.RenderToString();
    })
    .ContinueWith(r =>
    {
        if (r.IsCompleted)
            SaveOutput(r.Result);
    });

kick it on DotNetKicks.com

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • * Please enter your name
  • * Please enter a comment
  • Post
  • Thank you for submitting this cool story - Trackback from DotNetShoutout

  • You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Cok teşekkür ederim. Saygıalr.

Page 1 of 1 (3 items)