Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # Async
  2.  
  3. If we run an application then the code will run top-down, line by line.
  4. If an operation takes a long time then the program will 'hang' and appear
  5. to do nothing.
  6. Today we want 'responsive' apps which don't 'hang' unless there is a critical fault.
  7. 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.
  8.  
  9. C#
  10.  
  11. Main(){
  12. DoThisAsync(); // convention is use word 'Async' at END OF METHOD
  13. Console.ReadLine(); // ensure program stops and waits for async to
  14. complete. Otherwise application may terminate
  15. before async returns.
  16. }
  17.  
  18. async static void DoThisAsync(){
  19. await File.ReadAsync("thisfile.txt"); // marks line where aysync
  20. // operation takes place
  21. }
  22.  
  23. async keyword ensures that the 'Main' method now does not WAIT for this
  24. method but the processing just calls the method then continues processing
  25. the next line of the main method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement