与-的比较和选择指南-ValueTask-异步编程-Task-.NET-深入 (と与や的比较)
In C, a task represents an asynchronous operation. It is commonly used to encapsulate time-consuming and asynchronous operations such as reading data from a file or executing a database query.
Properties of a Task
- Status: Indicates the current state of the task (e.g., running, completed, canceled).
- Result: Contains the result of the asynchronous operation (if successfully completed).
- Exception: If the asynchronous operation encounters an exception, it is stored here.
- IsCompleted: Returns true if the task has completed (either successfully or with an exception).
- IsFaulted: Returns true if the task completed with an exception.
Creating Tasks
There are several ways to create tasks in C:
- Task.Run(): Creates a task from a delegate or lambda expression.
- Task.Factory.StartNew(): Similar to Task.Run(), but provides more control over task creation.
- async/await: Asynchronous programming model that simplifies task creation and handling.
Consuming Tasks
Once a task has been created, it can be consumed using various methods:
- Task.Wait() and Task.Result: Blocks the calling thread until the task completes and returns the result.
- Task.ContinueWith(): Creates a continuation task that executes when the original task completes.
- await: Asynchronous programming model that avoids blocking the calling thread.
Cancellation and Faults
Tasks support cancellation and error handling:
- Cancellation: A task can be canceled, which will stop its execution and result in a 'canceled' status.
- Faults: If an asynchronous operation encounters an exception, the task will be marked as 'faulted' and the exception will be stored in the Exception property.
Benefits of Using Tasks
- Asynchronicity: Allows long-running operations to be performed without blocking the calling thread.
- Concurrency: Enables simultaneous execution of multiple tasks, improving application performance.
- Error Handling: Provides a structured way to handle exceptions in asynchronous operations.
- Composition: Tasks can be combined using continuation tasks to create complex workflows.
Conclusion
Tasks are a powerful tool in C for representing and managing asynchronous operations. They enable developers to create efficient and responsive applications that can perform time-consuming tasks without blocking the UI or other critical processes.
如何正确理解.NET 4.5和C#5.0中的async/await异步编程模式
相对于之前Begin/End模式和事件模式,async/await模式让程序员得以用同步的代码结构进行异步编程。async/await入门很方便,但是深入理解却涉及很多领域,如线程池、同步上下文等等。我断断续续接触了几个月,稍微有一些心得:
await的作用是等待异步Task完成,并不是阻塞的。举个例子,一个异步方法:
1
如何正确理解.NET 4.5和C#5.0中的async/await异步编程模式这个await,其实只是把对老版本C#迭代器的惯用法官方化了。 现在很多平台都因为一些原因不得不用旧版本的C#,比如unity,想异步那只能通过迭代器来做。 async、迭代器都是语法糖,编译器会帮你实现成一个状态机匿名类,实例里面hold住一些临时变量,记录一下当前状态。 根据你写的yield/await,把一个异步方法拆成几个同步block,根据一定规则定期的去MoveNext一下,Current是Task那我就根据你配置的线程上下文决定把这个Task跑在哪个线程上。 那么用await修饰的异步方法是在哪个线程中被调用的?为什么上面这个事件处理方法不会阻塞GUI?我还看到其它一些描述是说使用async/await异步模式不会生成新的线程,那么只在原来已有线程的基础上面如何做到异步运行?题主这个例子这个方法就是在UI线程调用的,并且没有ConfigureAwait(false),所以会在当前await时捕捉的UI线程上下文执行之后的同步block。 至于为什么不会阻塞,可以简单理解为执行了第一个block,碰到Delay(4000),给UI线程的定时器挂一个4000时间之后再调用下一个同步块的回调。 看题主说的书名像是国产的书,这方面还是看《CLR via C#》或者《Concurrency in C# cookbook》比较好。 |
免责声明:本文转载或采集自网络,版权归原作者所有。本网站刊发此文旨在传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及版权、内容等问题,请联系本网,我们将在第一时间删除。同时,本网站不对所刊发内容的准确性、真实性、完整性、及时性、原创性等进行保证,请读者仅作参考,并请自行核实相关内容。对于因使用或依赖本文内容所产生的任何直接或间接损失,本网站不承担任何责任。