调用异步方法不设置成员变量 [英] Calling async method does not set member variable
问题描述
这段代码是在 Visual Studio 2012 中用 C# 编写的,我使用的是 winrt 工具包
This code it´s write in C# in visual studio 2012 I ´ve using winrt tool kit
我尝试创建这个 loadGrupos 来捕获文件 grupo.txt 并读取他的内容并将其加载到类 Grupos 但我不知道发生了什么列表 grupos 从 json 接收内容但是当我打电话时loadGrupos 变量 grupos 不公开任何内容.我真的不知道这是怎么回事.我试过调试,没有发现任何错误.
Hi I tried create this loadGrupos to catch the file grupo.txt and read and load his content in to the class Grupos but I don´t know what´s happen the list grupos recive the content from the json but when I callend the loadGrupos the variable grupos don´t expose nothing. I really don´t know what´s going on. I tried debug and I didn´t found any error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Windows.Storage;
using Windows.ApplicationModel;
using WinRTXamlToolkit.IO.Extensions;
using Windows.UI.Popups;
namespace xxx;
public class MyJConverter
{
public String _Path;
//private Windows.ApplicationModel.Package package;
// private Windows.Storage.StorageFolder installedLocation;
private StorageFolder _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
//KnownFolders.DocumentsLibrary
public MyJConverter() {
// package = Windows.ApplicationModel.Package.Current;
// installedLocation = package.InstalledLocation;
}
public void save(Object obj)
{
_Path = JsonConvert.SerializeObject(obj);
gravandoUmDocumento("data",_Path);
}
public void saveGrupo(Object obj)
{
_Path = JsonConvert.SerializeObject(obj);
gravandoUmDocumento("grupo", _Path);
}
public async void gravandoUmDocumento(String nomeDoArquivo,String menssagem) {
// var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
// var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
//await menssagem.WriteToFile(nomeDoArquivo + "1.txt", KnownFolders.DocumentsLibrary);
await menssagem.WriteToFile(nomeDoArquivo + ".txt", _Folder);
}
private List<Ano> anos;
public List<Ano> load()
{
leituraDeUmArquivo("data").Wait();
if (anos != null)
{
return anos;
}
return null;
}
private List<Grupos> grupos;
public List<Grupos> Grupos
{
get { return grupos; }
set { grupos = value; }
}
public List<Grupos> loadGrupos()
{
leituraDeUmArquivo("grupo").Wait();
{
if (grupos != null)
{
return grupos;
}
else
return null;
}
}
public async Task leituraDeUmArquivo(String arquivoASerLido)
{
String leitura = "";
//leitura do data
try
{
// settings
var _Path =arquivoASerLido + ".txt";
// acquire file
var _File = await _Folder.GetFileAsync(_Path);
// read content
leitura = (String) await Windows.Storage.FileIO.ReadTextAsync(_File);
// leitura = (String)_ReadThis;
}
catch {
// leituraDeUmArquivo(arquivoASerLido);
}
if (leitura != "")
{
if (arquivoASerLido.Equals("data"))
{
try
{
anos = JsonConvert.DeserializeObject<List<Ano>>(leitura);
}
catch {
}
}
if (arquivoASerLido.Equals("grupo"))
{
try{
grupos = JsonConvert.DeserializeObject<List<Grupos>>(leitura);
}
catch { }
}
}
}
}
我做了你推荐的修改,但问题没有解决,所以我发布了我所有的代码.我真的没有找到为什么 winrt 无法加载文件,有时 win 8 加载有时没有.现在修改应用程序块并转到前面.
HI I did the modifications you recomend but the problem didn´t solved so I post all my code. I really didn´t found why the winrt can´t load the file, some time win 8 load some times no. Now with the modificiations the app block and do go to front.
如果我去调试视觉找到文件,如果我去只运行没有.
if I go with debug the visual found the file, if I go only run no.
推荐答案
应该有关于在不等待的情况下调用异步方法"(或类似的东西)的警告.
There should be warning about "calling async method without awaiting" (or something like this).
您需要await
来完成async
方法,而不仅仅是调用它.
You need to await
for completion of async
method instead of just calling it.
即廉价和肮脏(WinRT 不是个好主意)方法是调用 Task.Wait关于leituraDeUmArquivo
的结果(需要更新方法返回Task
):
I.e. cheap and dirty (not good idea for WinRT) approach is to call Task.Wait on result of leituraDeUmArquivo
(the method need to be updated to return Task
):
leituraDeUmArquivo("grupo").Wait();
更好的方法是使代码正确异步,只需 await
调用 leituraDeUmArquivo
(您的 loadGrupos
函数可能需要标记为 async
也是如此,并且处理得当).
Better approach is to make code properly asynchronous and just await
call to leituraDeUmArquivo
(your loadGrupos
function will likely need to be marked async
too and handled appropriately).
注意:要使 await
/.Wait()
工作 leituraDeUmArquivo
应该返回 Task
.
Note: to make await
/ .Wait()
working leituraDeUmArquivo
should return Task
.
public async Task leituraDeUmArquivo(String arquivoASerLido)
// don't need to touch body of the method.
这篇关于调用异步方法不设置成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!