What is 'await' looking for !

1:47 AM Shashank Tiwary 0 Comments

The way of running asynchronous operation from C# 5.0 is now has dramatic changes. Specifically there are two new operators async and await introduce by C# 5.0 language to support linear asynchronous programming model.

async

operator use as a modifier to specify that a method, lambda expression, or anonymous method is asynchronous.If you use this modifier on a method or expression, it's referred to as an asynchronous method.

await

operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.

We are going to look into the internals of how await operator works,
C# code uses await operator,
public async void DoSomething()
{
    string result = await obj.GetResultAsync(); // return immediately to calling code
    Console.WriteLine(result); // execution continue from here once above work completed 
}
A pseudo code generated by compiler is give below, When an await operator encounter by compiler.
public async void DoSomething()
{
    // obj - any object that have GetAwaiter method with appropriate return type
    var temp = obj.GetAwaiter(); 
    if (!temp.IsCompleted)
    {
        SAVE_STATE()
        temp.OnCompleted(&cont);
        return;
        cont:
        RESTORE_STATE()
        result = temp.GetResult();
        Console.WriteLine(result); // code below await expression
    }
}
snippet taken from (http://stackoverflow.com/questions/12661348/custom-awaitables-for-dummies)

Here program try to get the awaiter instance from the asynchronous method. This instance temp has an IsCompleted boolean property member which is use to determine if asynchronous task execution completes. If it return false the program store the current machine state and inject an Action (&cont) delegate by using OnCompleted() method of the awaiter instance. Note that this method is part of INotifyCompletion interface. Once the blocking operation execution completes program restore the machine state and get output result by calling GetResult() on awaiter instance. Further program continue below the await statement where its left control.

Condition that compiler check when we use await operator

  1. It check if the return type of await expression does have GetAwaiter() method. This method can also be a extension method for the instance type.
  2. GetAwaiter() must return type that have boolean type property IsCompleted.
  3. Return type of GetAwaiter() must implement INotifyCompletion interface.
  4. Return type of GetAwaiter() must have GetResult() method that return instance of expected type by await expression

0 comments:

A Search extension on DataTables

8:08 AM Shashank Tiwary 0 Comments


Hi, This article is the second part of my article grid in mvc using datatables . As this article is a bit more coupled with the first one therefore I recommend you to go through the first one before the second article. Its up to you.

While DataTables has a lot of flexibility built-in by default, there might come a time when you wish to add your own features or override some existing one provided by DataTables. This is possible through the plug-in architecture that DataTables provides for developers.

As we know, DataTables only provide a simple text box for query the data. We now going to convert this search box to do something interesting for us. So I write an extension to provide users with option of searching data for different columns at the same time. What our multiple search extension does is when some one click on search text box of DataTables, it open a search dialog box with configured check boxes and corresponding text boxes. One can just need to tick out the fields and type the query value. Now when user press return key the extension search the data table by calling dataTables API function "fnFilter".

The extension does one more interesting thing it set search parameters so that server can get the query data from the request object. Now again we come back to our debtors table and set our multiple search extension.

Here use case of my custom search plugin

oTable = $('#tableid').dataTable({
                "bProcessing": true,
                "bServerSide": true,
                "sServerMethod": "POST",
                "sAjaxSource": "/data/debtors",
                "aoColumnDefs": [
                    { "mDataProp": "Debtor", "aTargets": [0] },
                    { "mDataProp": "Iban", "aTargets": [1] },
                    { "mDataProp": "Bic", "aTargets": [2] }
                ],
                "aaSorting": [[1, 'desc']]
               });
oTable.fnAddSearchBox($("#exampleSearch"),
                        {
                          option:{"1":{ tip:'please enter IBAN'},
                                  "2":{ tip:'please enter BIC'}}
                        });
The search dialog html code

<div id="exampleSearch"  title="Search">
    <div>
        <input id="Name" type="checkbox" value="1" />
        <label class="sfieldslbl">Iban</label>
    </div>
    <div>
        <input id="Address" type="checkbox" value="2" />
        <label class="sfieldslbl">Bic</label>
    </div>
</div>

Look at jsbin cast ... JS Bin

0 comments: