MongoDB 无尽的查找 ToListAsync
MongoDB endless Find ToListAsync
我正在尝试从 MongoDB 集合中检索数据,但是发生了一些奇怪的事情。如果我显示 MessageBox,则数据提取有效,如果我不显示,则数据提取无效。
static class MongoDBController {
static MongoClient client = new MongoClient("mongodb://localhost");
public static async Task<List<Course>> GetCourses(string dbName = "school") {
// Get our course collection
var db = client.GetDatabase(dbName);
var collection = db.GetCollection<Course>("courses");
// Create an empty filter
var filter = new BsonDocument();
// Use the empty filter to get all courses from the database
return await collection.Find(filter).ToListAsync();
}
}
上面的代码从数据库中获取内容,下面的代码 - 在我的 Form1.cs
中找到 - 将它放在 ListBox 中。
private void FillCourseList() {
Task<List<Course>> courseTask = MongoDBController.GetCourses();
MessageBox.Show("Fetching worked (yay!)");
// get the result
List<Course> result = courseTask.Result;
// Show every course found in the resultset
foreach (Course s in result) {
listBox_overview_vakken.Items.Add(s);
}
}
现在,如果我删除 Fetching worked(耶!) 弹出窗口,我的 listBox 将永远不会被填充。
我做错了什么?
正如 Alex 友善指出的那样,该问题的解决方案是使 FillCourseList
也异步。这允许程序在从数据库中获取数据时继续 运行。我之前的阻塞呼叫显然是问题的原因。不过,这确实将 thread-safe calls 添加到 Windows 表单中。
private delegate void SetListCallback(List<Course> result);
private async Task GetCourseList() {
Task<List<Course>> courseTask = MongoDBController.GetCourses();
List<Course> result = await courseTask.ConfigureAwait(false);
// When finished, fill the listbox
FillCourseList(result);
}
private void FillCourseList(List<Course> result) {
// If the calling thread's ID doesn't match the creating thread's ID
// Invoke this method on the correct thread via the delegate
if (this.listBox_overview_vakken.InvokeRequired) {
SetListCallback d = new SetListCallback(FillCourseList);
this.Invoke(d, result);
} else {
foreach (Course s in result) {
listBox_overview_vakken.Items.Add(s);
}
}
}
我正在尝试从 MongoDB 集合中检索数据,但是发生了一些奇怪的事情。如果我显示 MessageBox,则数据提取有效,如果我不显示,则数据提取无效。
static class MongoDBController {
static MongoClient client = new MongoClient("mongodb://localhost");
public static async Task<List<Course>> GetCourses(string dbName = "school") {
// Get our course collection
var db = client.GetDatabase(dbName);
var collection = db.GetCollection<Course>("courses");
// Create an empty filter
var filter = new BsonDocument();
// Use the empty filter to get all courses from the database
return await collection.Find(filter).ToListAsync();
}
}
上面的代码从数据库中获取内容,下面的代码 - 在我的 Form1.cs
中找到 - 将它放在 ListBox 中。
private void FillCourseList() {
Task<List<Course>> courseTask = MongoDBController.GetCourses();
MessageBox.Show("Fetching worked (yay!)");
// get the result
List<Course> result = courseTask.Result;
// Show every course found in the resultset
foreach (Course s in result) {
listBox_overview_vakken.Items.Add(s);
}
}
现在,如果我删除 Fetching worked(耶!) 弹出窗口,我的 listBox 将永远不会被填充。
我做错了什么?
正如 Alex 友善指出的那样,该问题的解决方案是使 FillCourseList
也异步。这允许程序在从数据库中获取数据时继续 运行。我之前的阻塞呼叫显然是问题的原因。不过,这确实将 thread-safe calls 添加到 Windows 表单中。
private delegate void SetListCallback(List<Course> result);
private async Task GetCourseList() {
Task<List<Course>> courseTask = MongoDBController.GetCourses();
List<Course> result = await courseTask.ConfigureAwait(false);
// When finished, fill the listbox
FillCourseList(result);
}
private void FillCourseList(List<Course> result) {
// If the calling thread's ID doesn't match the creating thread's ID
// Invoke this method on the correct thread via the delegate
if (this.listBox_overview_vakken.InvokeRequired) {
SetListCallback d = new SetListCallback(FillCourseList);
this.Invoke(d, result);
} else {
foreach (Course s in result) {
listBox_overview_vakken.Items.Add(s);
}
}
}