DQDQDQbJAB%{`gzy{K|K9'R{Py5u'}{wj9=My']}b'5u?zPy'iy{up9LugDLuyqLuPyu9^9{wiyiymx'KRRR]DQDQbJA%R{`F{PMy5u'}M?cF{giy'}?i{pPiy|iy|iyPhyOh^KDQDQbJA?crAriaABbAEAfrDCCX~C-~~~| G56864987589664FF55F55WC-~B~C 6647954655V 4VXY~CZZWW~BX

Async.` A river rushes along with no interruptions. Now imagine another river some distance away. It too flows with no interruptions. A bird's song can be heard.`With async and await` in C# we call functions in an asynchronous way. Like the rivers these tasks can run with no interference. Many things happen at once. `Task: `A Task returns no value (it is void). A Task<int> returns an element of type int. This is a generic type.`An async method will be run synchronously if it does not contain the await keyword.`First program.` We use the async and await keywords to asynchronously run a method. The program begins a long-running method (HandleFileAsync). `First: `We create a Task instance by calling the HandleFileAsync method. The task starts, and we call Wait() for it to finish.`Messages: `The method displays a status message after it starts. When it ends, the results are displayed.`HandleFileAsync: `In HandleFileAsync, we use the StreamReader type and await the ReadToEndAsync method. We perform some computations.`Above,` the slow computation done in HandlFileAsync is for demonstration. If you change the path to a large text file that exists on your computer, the program should work. `We can do something (such as write a message) after the async method starts. This is not possible with synchronous methods.`Important: `We must be careful to call Wait() on the task we want to wait for. Sometimes the wrong Task can be waited on.`Simple example.` This program runs a computation asynchronously on every line entered in the console. It keeps accepting lines even when computations are running. `Action: `A lambda expression is specified as the argument to Task.Run. This is an action delegate.`Action `action`Allocate: `This method does a slow-running computation. But when run asynchronously, it does not cause the program to freeze.`Result: `Many user inputs can be handled while the computation is running. Each Allocate() call finishes at its own pace.`Main method.` The async keyword cannot be used on the Main method. So we will need to add a second method before using an await call. `A pattern.` Async and await are a code pattern—they allow methods to asynchronously run. They are a form of syntactic sugar. They make code that uses threads easier to read.`Complexity.` With async and await, the compiler helps with asynchronous code. We return a Task or void from an async method. Visual Studio reports warnings or errors on incorrect methods.`Types` (StreamReader, HttpClient) contain "Async" methods. These should be called with the await keyword. And the await keyword must be used within an async method. `StreamReader `streamreader`HttpClient `httpclient`Task.Start: `The first async method call can occur with the Task Start method. This is an instance method.`Event handlers can be used with async methods. This is not currently shown here.`ReadToEndAsync.` When can ReadToEndAsync lead to a performance gain in a program? When we have high CPU usage with a file load, we can speed up completion time with async and await. `StreamReader ReadToEndAsync `streamreader-readtoendasync`Asynchronous.` This term does not mean multithreaded code. By default, code written with async and await is single-threaded. But threaded code works well here. `Threads `thread`Task.Run.` With the Task.Run method, we can make code that uses async and await multithreaded. Asynchronous code is code that returns upon completion. `Other code can execute (even on the same thread) after an asynchronous task has started.`Some corrections.` Thanks to Donnie Karns for pointing out that async code statements are not (by default) run on another thread. Thanks to Jim Boyer for correcting a "nested" task problem. `Thanks to Andrew Dennison for reviewing the Allocate() method code which had an unnecessary statement.`Quote: `The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread.`Async, await: MSDN `https://msdn.microsoft.com/en-us/library/hh191443.aspx`Concept.` Programs are full of methods that do not immediately return. Sometimes an external slowdown, as from a network, is the cause, not processor usage.`With these keywords,` we run methods in an asynchronous way. Threads are optional. This style of code is more responsive. A network access can occur with no program freeze.

XZ^ ZZ; ZZ.IO; ZZ.Threading.TasksZZZ { XZZX{^ XXZZ the HandleFile mZ. XX^Task<Z>^ taskZHandleFileAsync();^ XXZControl Zs here beZe HandleFileAsync Zs. XXZPrompt the user. XX^Z^"Please wait patiently "^ + XXX^"Z I do something important."^);^ XXZDo something at the same time as the file is being read. XX^Z lineZZ.ZLine(); XXZ^"You entered (asynchronous logic): "^Zline);^ XXZWait Zthe HandleFile taskZcomplete. XXZZ its Zs. XX^task.^Wait^(); XXvar xZtask.Result; XXZ^"Z: "^Zx)ZXXZ^"[DONE]"^); XXZ.ZLine(); X} XZ^async^ ^Task<Z>^ HandleFileAsync() X{ XXZ fileZ^@"C:\programs\enable1.txt"^; XXZ^"HandleFile enter"^); XXZcountZ0;^ XXZZZthe specified file. XXZUse async ZZer mZ. XX^Z(ZZer readerZZZZer(file)) XX{ XXXZ vZ^await^ reader.ZToEndAsync();^ XXXZProcess the file data somehow. XXX^count += v.LZ;^ XXXZA slow-running computation. XXXZXDummy code. XXX^Z(ZiZ0; i < 10000; i++) XXX{ XXXXZxZv.GetHashCode(); XXXXZxZ0) XXXX{ XXXXXcount--; XXXX} XXX} XX} XXZ^"HandleFile exit"^); XXZ count; X} } ^Output: initial^ HandleFile enter Please wait patiently Z I do something important. ^Output: part 2^ HandleFile enter Please wait patiently Z I do something important. ^test^ You entered (asynchronous logic): ^test^ ^Output: final^ HandleFile enter Please wait patiently Z I do something important. ^test^ You entered (asynchronous logic): ^test^ HandleFile exit Z: 1916146 [DONE]^ ZZ; ZZ.Threading.TasksZZZ { XZX{ XXZ (true) XX{^ XXXZZ computation. XXX^EZ();^ XXXZHandle user input. XXX^Z ZZZ.ZLine(); XXXZ^"You typed: "^ZZ); XX} X} XZ^async^ ZEZ() X{^ XXZThis mZ runs asynchronously. XX^ZtZ^await^ ^Task.Run^(() => Allocate()); XXZ^"Compute: "^Zt); X} XZZAllocate() X{^ XXZCompute total count of digitsZZs. XX^ZsizeZ0; XX^Z^ (ZzZ0; z < 100; z++) XX{ XXX^Z^ (ZiZ0; i < 1000000; i++) XXX{ XXXXZ ZZi.ToZ(); XXXXsize += Z.LZ; XXX} XX} XXZ size; X} } ^ hello You typed: hello good You typed: good day You typed: day Compute: 588889000 friend You typed: friend Compute: 588889000 Compute: 588889000 Compute: 588889000 Compute: 588889000^ ZZ; ZZ.Threading.TasksZZZ { XZ^async^ ZZ() X{ X} } ^ error CS4009: 'Z.Z()': an entry poZcannot be marked Xwith the 'async' modifier^

#ba9async, await, Taskasync computationcauses compile-time error