【发布时间】:2016-03-24 22:36:03
【问题描述】:
我正在尝试等待异步函数完成,以便在我的 UI 线程中填充 ListView。
这是代码
public Form1()
{
InitializeComponent();
Task t = new Task(Repopulate);
t.Start();
// some other work here
t.Wait(); //completes prematurely
}
async void Repopulate()
{
var query = ParseObject.GetQuery("Offer");
IEnumerable<ParseObject> results = await query.FindAsync();
if (TitleList == null)
TitleList = new List<string>();
foreach (ParseObject po in results)
TitleList.Add(po.Get<string>("title"));
}
Form1() 中的 TitleList = null 因为 Repopulate() 尚未完成。因此我使用了Wait()。但是,在函数完成之前等待就返回了。
我在这里做错了什么?
【问题讨论】:
标签: c# .net async-await task-parallel-library