Now, consider those same instructions written as C# statements: using System Each cook (or thread) would be blocked synchronously waiting for the bacon to be ready to flip, or the toast to pop. Each one would be focused on just that one task. One would make the eggs, one the bacon, and so on. Once the bacon starts, you can put the bread into the toaster.įor a parallel algorithm, you'd need multiple cooks (or threads). As soon as you start warming the pan for the eggs, you can begin frying the bacon. The cooking progresses whether or not someone is watching it. Continuing the breakfast analogy, one person can make breakfast asynchronously by starting the next task before the first task completes. One person (or thread) can handle all these tasks. At each step of the process, you'd start a task, then turn your attention to tasks that are ready for your attention.Ĭooking breakfast is a good example of asynchronous work that isn't parallel. You'd put the bread in the toaster, then start the eggs. You'd start warming the pan for eggs, then start the bacon. If you have experience with cooking, you'd execute those instructions asynchronously. You'd write the instructions something like the following list to explain how to make a breakfast: Throughout this article, you'll use an example of instructions for making breakfast to see how the async and await keywords make it easier to reason about code that includes a series of asynchronous instructions. It's analogous to how people give instructions for processes that include asynchronous tasks. That's the goal of this syntax: enable code that reads like a sequence of statements, but executes in a much more complicated order based on external resource allocation and when tasks are complete. The compiler performs many transformations because some of those statements may start work and return a Task that represents the ongoing work. You can read that code as though each statement completes before the next begins. You write code as a sequence of statements, just like always. The Task asynchronous programming model (TAP) provides an abstraction over asynchronous code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |