C# 5.0

345 views
263 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
345
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • if you have a synchronous method that returns void and all you do to change it is mark it as “async”, invocations of that method will still run synchronouslySimilarly, if you have a synchronous method that returns some TResult, and all you do is mark it as “async” and change the return type to be “Task<TResult>”, invocations of that method will still run synchronously.if “await” isn’t used at all in a method’s code, there will only be one piece, and since that piece will start running synchronouslyIAwaiter an interface that contains callback methods
  • Because Task has GetAwaiter Method
  • For example: If you have a single Await keyword in your aysnc method. Then the compiler will create the state machine with 2 methods. One will contains the code from the first line of the method till Await line and the other method will contain the rest of the logic. Then the compiler executes the first method first and then move to main method from where it calls that async method and then when the awaited task (which was fired in the first method) returns the response then it calls the second method
  • - http://weblogs.asp.net/dixin/archive/2012/11/08/understanding-c-async-await-2-awaitable-awaiter-pattern.aspxhttp://enumeratethis.com/2010/11/03/c-5-0-rx-schedulers/InitializeCancellationTokeSouce global object under async method and then call its Cancel method when you want to cancel the on going taskAwait Task.Whenall(<list of tasks>)
  • C# 5.0

    1. 1. C # 5.0 New Features By S. M. Ali Raza Naqvi Senior Software Engineer
    2. 2. Agenda Basic introduction to Async and Await keywords Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute (if we got time)
    3. 3. Async & Await A keyword use to create async methods Async method must contains ‘Await’ keyword Async methods invoke synchronously Await is the point where method suspend and resume asynchronously Purpose of Await keyword is to wait for the task to complete
    4. 4. Async & Await (Cont.) Only methods that return void, Task or Task<Tresult> can mark as Async Methods which can’t mark async: Main, method implementing synchronized attribute or security Await can’t be use in methods that are not mark Async Inside Properties Inside lock, catch or finally block
    5. 5. Async & Await - Flow Output:
    6. 6. Async & Await - Behind the scene Compiler uses state machine to track the current state of the flow State machine holds the parameters, local variables and the current state of the method When encounter await, It stores state machine object as variable and resume it when awaiter gets the response
    7. 7. Async & Await - Ease Readable and maintainable code Allow you to write asynchronous code in synchronous fashion Exception handling using try..catch block Exceptions with much more details Allow you to use “using” block
    8. 8. Async & Await - Moreover You can create customize awaitable types using IAwaitable interface You can cancel an existing ongoing task using CancellationTokenSource object You can deal with multiple task all at once and can use await for all of them at a time
    9. 9. Lets see the Code
    10. 10. Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute You can obtain information about the caller to a method To help in tracing CallerFilePath – return the caller’s file location CallerLineNumber – return the line number from where that method called CallerMemberName – return the name of the method that calls that method
    11. 11. Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute (Cont.) It don’t involve any reflection At compile time

    ×