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; }
我已经搜索了很长时间,寻找答案,但是其他所有人都只是提出了更多异步方法,这对我来说不是一个选择。谢谢。
创建一个异步任务,运行方法并使用它,而不是直接调用方法。