MongoDB无尽的查找ToListAsync [英] MongoDB endless Find ToListAsync

查看:175
本文介绍了MongoDB无尽的查找ToListAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MongoDB集合中检索数据,但是正在发生一些奇怪的事情.如果显示MessageBox,则数据获取有效,如果不显示,则数据获取无效.

I'm attempting to retrieve data from a MongoDB collection, however something strange is happening. If I show a MessageBox the data fetch works, if I don't it doesn't.

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中.

The code above gets the content out of the database, the code below - found in my Form1.cs - places it inside a 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正常工作(是的!)弹出窗口,则listBox永远不会被填充.

Now if I remove the Fetching worked (yay!) pop-up my listBox never gets filled.

我在做什么错了?

推荐答案

正如Alex所指出的那样,该问题的解决方案也在使FillCourseList异步化.这允许程序在从数据库中获取数据的同时继续运行.我之前遇到的阻塞呼叫显然是造成此问题的原因.这确实增加了对线程安全调用移至Windows窗体.

The solution to the issue, as Alex kindly pointed out, is making the FillCourseList asynchronous aswell. This allows the program to continue running while the data is fetched from the database. The blocking call I had before apparently was the cause of the issue. This does add the need for thread-safe calls to the Windows Form though.

    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无尽的查找ToListAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆