sql >> Base de Datos >  >> NoSQL >> MongoDB

MongoDB interminable Buscar ToListAsync

La solución al problema, como señaló amablemente Alex, es hacer que FillCourseList asíncrono también. Esto permite que el programa continúe ejecutándose mientras se obtienen los datos de la base de datos. La llamada de bloqueo que tuve antes aparentemente fue la causa del problema. Esto agrega la necesidad de thread-safe llamadas aunque a Windows Forms.

    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);
            }
        }
    }