Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Async
- If we run an application then the code will run top-down, line by line.
- If an operation takes a long time then the program will 'hang' and appear
- to do nothing.
- Today we want 'responsive' apps which don't 'hang' unless there is a critical fault.
- Examples mainly include online/offline apps which have sensitivity to the network status. If the app goes offline, it can either stop working or revert to an offline mode.
- C#
- Main(){
- DoThisAsync(); // convention is use word 'Async' at END OF METHOD
- Console.ReadLine(); // ensure program stops and waits for async to
- complete. Otherwise application may terminate
- before async returns.
- }
- async static void DoThisAsync(){
- await File.ReadAsync("thisfile.txt"); // marks line where aysync
- // operation takes place
- }
- async keyword ensures that the 'Main' method now does not WAIT for this
- method but the processing just calls the method then continues processing
- the next line of the main method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement