使用 await 时读取文本文件和代码跳转
Reading a text file and code jumps when using await
我正在为 windows 8.1 平板电脑编写一个应用程序,我正在尝试从我保存的文本文件中读取数据(这个文本文件刚刚超过 1kb)。下面的代码我在某些情况下工作但主要是失败(debugging/stepping 在代码上经常会看到它成功)。
StorageFolder folder = ApplicationData.Current.LocalFolder;
string fileName = "collections.json";
public Dictionary<string, string> masterDataObject;
private async void CallLoad()
{
await this.Load();
}
public async Task<Dictionary<string, string>> Load()
{
//create a file
var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
string fileContents = string.Empty;
if (file != null)
{
//returns a JSON string
fileContents = await FileIO.ReadTextAsync(file);//WHY DOES IT JUMP AT THIS LINE?????
}
Dictionary<string, string> customerFromJSON = JsonCrt.DeserializeObject<Dictionary<string, string>>(fileContents) ?? new Dictiy<string, string>();
masterDataObject = customerFromJSON;
return masterDataObject;
}
在多次执行代码后,我可以在行中看到
文件内容=等待.....
它退出方法并继续执行不在我的 Load() 方法中的其余代码。
我已经回顾了 async 和 await 并真正尝试去理解它,但我的理解让我对为什么会这样感到困惑。
我相信 await 意味着执行将停止,直到您从正在调用的代码中得到响应(显然不是)。
那么我应该如何使用 async 使其正常工作,或者有比这段代码更好的选择吗?
我查看并尝试了很多选项,但都没有成功,如有任何建议,我们将不胜感激。
await 在幕后做了什么:
- 它创建一个包含代码 after await
的回调
- 它调用
xyzAsync()
函数,注册回调。
- 如果异步操作完成,则调用回调。
因此,如果您使用调试器逐步执行代码,它会在遇到 await 时执行一些其他代码,返回到 await[= 之后的代码22=] 一旦异步操作完成。
it drops out of the method and continues with the rest of the code that is not in my Load() method
这正是它要做的,将控制权交给调用站点,直到异步操作完成。
我假设你的问题在于你的 CallLoad
方法是 async void
而不是 async Task
,因此无法等待,这就是你看到它继续的原因无需等待内部异步 IO 完成。
I was under the belief that await meant that execution would stop until you get a response from the code you are calling
这仍然完全正确,如果你等待异步操作一直
So how should I be using async to make it work properly?
async void
仅适用于顶级事件处理程序。如果 CallLoad
未用作一个,则应为 async Task
并等待:await CallLoad();
我正在为 windows 8.1 平板电脑编写一个应用程序,我正在尝试从我保存的文本文件中读取数据(这个文本文件刚刚超过 1kb)。下面的代码我在某些情况下工作但主要是失败(debugging/stepping 在代码上经常会看到它成功)。
StorageFolder folder = ApplicationData.Current.LocalFolder;
string fileName = "collections.json";
public Dictionary<string, string> masterDataObject;
private async void CallLoad()
{
await this.Load();
}
public async Task<Dictionary<string, string>> Load()
{
//create a file
var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);
string fileContents = string.Empty;
if (file != null)
{
//returns a JSON string
fileContents = await FileIO.ReadTextAsync(file);//WHY DOES IT JUMP AT THIS LINE?????
}
Dictionary<string, string> customerFromJSON = JsonCrt.DeserializeObject<Dictionary<string, string>>(fileContents) ?? new Dictiy<string, string>();
masterDataObject = customerFromJSON;
return masterDataObject;
}
在多次执行代码后,我可以在行中看到 文件内容=等待..... 它退出方法并继续执行不在我的 Load() 方法中的其余代码。
我已经回顾了 async 和 await 并真正尝试去理解它,但我的理解让我对为什么会这样感到困惑。
我相信 await 意味着执行将停止,直到您从正在调用的代码中得到响应(显然不是)。
那么我应该如何使用 async 使其正常工作,或者有比这段代码更好的选择吗?
我查看并尝试了很多选项,但都没有成功,如有任何建议,我们将不胜感激。
await 在幕后做了什么:
- 它创建一个包含代码 after await 的回调
- 它调用
xyzAsync()
函数,注册回调。 - 如果异步操作完成,则调用回调。
因此,如果您使用调试器逐步执行代码,它会在遇到 await 时执行一些其他代码,返回到 await[= 之后的代码22=] 一旦异步操作完成。
it drops out of the method and continues with the rest of the code that is not in my Load() method
这正是它要做的,将控制权交给调用站点,直到异步操作完成。
我假设你的问题在于你的 CallLoad
方法是 async void
而不是 async Task
,因此无法等待,这就是你看到它继续的原因无需等待内部异步 IO 完成。
I was under the belief that await meant that execution would stop until you get a response from the code you are calling
这仍然完全正确,如果你等待异步操作一直
So how should I be using async to make it work properly?
async void
仅适用于顶级事件处理程序。如果 CallLoad
未用作一个,则应为 async Task
并等待:await CallLoad();