C#如何等待异步任务以常规void方法完成[关闭]

问题描述 投票:-2回答:1
我有两种方法。一个是async Task,另一个是返回字符串数组的常规方法。但是,由于我的代码需要使用async Task方法,因此放置在实现之后的其余代码将继续运行。我一直在谷歌搜索数小时,试图找到一种方法来等待正常的string[]方法中的异步任务。我不想在程序异步方法中使用所有方法。这是我的代码...

这是我的异步任务

public static async Task DownloadDropboxFile(DropboxClient client, string path, string localDest) { using (var response = await client.Files.DownloadAsync(path)) { using (var fileStream = System.IO.File.Create(localDest)) { (await response.GetContentAsStreamAsync()).CopyTo(fileStream); } } }

此方法调用DownloadDropboxFile()。我

不希望它成为async方法,我希望将await放在DownloadDropboxFile()前面。我想等待它完成,然后再进行其余代码。

public static string[] ReadAllLines(DropboxClient client, string path) { string local_file = AppDomain.CurrentDomain.BaseDirectory + @"\loaded_content\read_file\file.dat"; DownloadDropboxFile(client, path, local_file); //This method continues to run while the rest of the code takes place, resulting in an error where local_file does not exist. string[] result = System.IO.File.ReadAllLines(local_file); System.IO.File.Delete(local_file); return result; }
我已经搜索了很长时间,寻找答案,但是其他所有人都只是提出了更多异步方法,这对我来说不是一个选择。谢谢。
c# async-await task-parallel-library
1个回答
-1
投票
创建一个异步任务,运行方法并使用它,而不是直接调用方法。

-1
投票

创建一个异步任务,运行方法并使用它,而不是直接调用方法。

© www.soinside.com 2019 - 2024. All rights reserved.