当前位置:首页 > 数码 > 与-的比较和选择指南-ValueTask-异步编程-Task-.NET-深入 (と与や的比较)

与-的比较和选择指南-ValueTask-异步编程-Task-.NET-深入 (と与や的比较)

admin5个月前 (04-30)数码19

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:

  1. Task.Run(): Creates a task from a delegate or lambda expression.
  2. Task.Factory.StartNew(): Similar to Task.Run(), but provides more control over task creation.
  3. 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:

ValueTask
  • 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》比较好。

免责声明:本文转载或采集自网络,版权归原作者所有。本网站刊发此文旨在传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及版权、内容等问题,请联系本网,我们将在第一时间删除。同时,本网站不对所刊发内容的准确性、真实性、完整性、及时性、原创性等进行保证,请读者仅作参考,并请自行核实相关内容。对于因使用或依赖本文内容所产生的任何直接或间接损失,本网站不承担任何责任。

标签: .NET

“与-的比较和选择指南-ValueTask-异步编程-Task-.NET-深入 (と与や的比较)” 的相关文章

3.1-8-.NET-Core-从-.NET-无缝更新到-摸索全面变更指南 (318川藏线)

3.1-8-.NET-Core-从-.NET-无缝更新到-摸索全面变更指南 (318川藏线)

Core3.1曾经用了很长一段期间,其真实2022年的年底微软曾经不提供支持了,前面的一个LTS版本.NET6也会在2024年11月中断支持,所以间接更新到.NET8是最好的选用。 微软官网...

揭秘其在.NET运行中灵敏读取和修正Excel文件的弱小性能-NPOI库深度解析 (揭秘揭秘大揭秘)

揭秘其在.NET运行中灵敏读取和修正Excel文件的弱小性能-NPOI库深度解析 (揭秘揭秘大揭秘)

一、NPOI库简介 NPOI(Non-ProfitOpenSourceSoftwarefor)是一个开源的.NET库,用于读取和写入Microsoft格局文件,包括、和PowerPoint等...

作为-的原因-Python-开发人员-我开始使用-.NET (作为的原因)

作为-的原因-Python-开发人员-我开始使用-.NET (作为的原因)

作者:Alex Maher | 编译:小欧 作为一名开发人员,很长一段时间以来,我一直关注 C 和 .NET 的出色工具和功能。但我最近开始使用 Python,感觉非常棒。这里申明一点,文章...

.NET-以及内存机制深化详解-中高效字符串存储指南-常量与灵活字符串解析

.NET-以及内存机制深化详解-中高效字符串存储指南-常量与灵活字符串解析

在中,字符串是无法变的,这象征着一旦创立,字符串的内容就不能被修正。字符串在内存中以不同的模式存储,详细取决于它是常量字符串还是灵活创立的字符串。 常量字符串 常量字符串在编译时就被解...

开源的.NET散布式事务处置打算 (开源的内核架构)

开源的.NET散布式事务处置打算 (开源的内核架构)

前言 在散布式系统中,因为各个系统服务之间的独立性和网络通讯的不确定性,要确保跨系统的事务操作的最终分歧性是一项严重的应战。当天给大家介绍一个开源的处置散布式事务的处置打算基于.NETSta...

揭秘.NET-冲动人心的变动-8.0 (揭秘国安部点名的间谍机构)

揭秘.NET-冲动人心的变动-8.0 (揭秘国安部点名的间谍机构)

作者:葡萄城技术团队 链接: 1性能优化 .NET8在整个堆栈中带来了数千项性能改良。自动状况下会启用一种名为灵活性能文件疏导优化(PGO)的新代码生成器,它可以依据实...