简体   繁体   English

编写一个不会调用另一个异步方法的异步方法

[英]Write an async method that doesn't call another async method

I know how to write an async method that calls other async methods, such as DownloadFileAsync or ReadAsStringAsync . 我知道如何编写一个调用其他异步方法的异步方法,例如DownloadFileAsyncReadAsStringAsync How do I write one that doesn't use framework's built-in async functionality? 如何编写不使用框架的内置异步功能的代码?

I assume you mean to say that you want to implement your own asynchronous functionality. 我假设您是说要实现自己的异步功能。 Calilng a method asynchronously just means the calling thread is not blocked. 异步调用方法仅表示未阻塞调用线程。 A case where this is useful is when you want to continue to do work while some background action completes. 当您希望在某些后台操作完成后继续进行工作时,此方法很有用。 There are various methods available in the framework that can be executed asyncronously (typically with the convention MethodNameAsync) as you mentioned. 如前所述,框架中有多种方法可以异步执行(通常使用MethodNameAsync约定)。 To fire off one of your own methods you can spin up your own thread and have it execute your code. 要触发您自己的方法之一,您可以启动自己的线程并让其执行您的代码。

For example. 例如。 TaskFactory.StartNew can be used to launch your code by passing it an Action (delegate). 通过将Action (委托)传递给TaskFactory.StartNew,可以用来启动代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM