简体   繁体   中英

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 . 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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