在其他线程中运行LINQ查询? [英] Running a LINQ Query in a different Thread?

查看:96
本文介绍了在其他线程中运行LINQ查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在使用LINQ进行SQL查询时遇到了一些困难.

应用程序概述:我正在尝试汇总一个用于显示我的电影收藏的应用程序.在应用程序中,我有一个填充有图像的ListBox,这些图像的URL存储在SQL Server表中.

当我运行以下代码以获取包括URL的信息时,最终将显示图像.但是,查询将锁定应用程序UI,直到完成查询并使用Images填充ListBox.

Hi Guys, I am having some difficulty with a SQL Query using LINQ.

Application Overview: I am trying to put together an application for displaying my Movie Collection. In the Application, i have a ListBox that is populated with images, the URL''s to those images are stored in an SQL Server Table.

When i run the below code to fetch the Information including the URL, the Images are eventually displayed. However, the query locks up the application UI until completion of the query and population of the ListBox with the Images.

var query = from s in dc.movies
                        orderby s.movie_NAME ascending
                        select s;
               MovieListBox.ItemsSource = query.ToList();



问题:是否可以在不阻塞应用程序UI的情况下将大型项目添加到ListBox?

更多信息:我很确定下面的代码将在单独的线程中运行查询,但是我不认为这是问题所在.我认为问题在于立即将如此大量的图像添加到ListBox中.

也许一个接一个地添加图像可能是一种解决方案?



Question: Is there a way to add large items to a ListBox without clogging the application UI?

More Information: I am pretty sure the below code will run the query in a seperated thread but i don''t think that is the problem. I think the issue is with adding such a large amount of images to the ListBox at once.

Maybe adding the Images one by one could be a solution?

public void LoadMovies()
        {
            Thread thread = new Thread(ThreadLoadMovies);
            thread.Start();
        }
        private void ThreadLoadMovies()
        {
            var query = from s in dc.movies
                        orderby s.movie_NAME ascending
                        select s;
            MovieListBox.Dispatcher.Invoke(new Action(() => MovieListBox.ItemsSource = query.ToList()), DispatcherPriority.Normal, null);
        }


我刚读完问题,就知道它有多模糊,对此表示歉意,我想不出更好的书写方式.

感谢您的提前帮助,
Alex


I just read the question and i understand how vague it is and apologise for that, i can''t think of a better way of writing it.

Thanks for your help in advance,
Alex

推荐答案

使用BackgroundWorker调用在单独的线程中执行sql的方法.将LINQ的结果存储在类级别的字段中.使用BackgroundWorker的RunWorkerCompleted方法将项目加载到列表中,并使用BeginInit()和EndInit()包装加载,以防止在加载结束之前重新绘制.
Use BackgroundWorker to call a method that executes your sql in a separate thread. Store the result of the LINQ in a class-level field. Use the BackgroundWorker''s RunWorkerCompleted method to load the items into the list and wrap the loading with BeginInit() and EndInit() to prevent redrawing until the end of the load.


知道委托能够使用BeginInvoke和EndInvoke隐式创建线程吗?

http://www.dotnetfunda.com/articles/article807-asynchronous-method-invocation-. aspx [ ^ ]
Do you know delegates are capable of creating threads implicitely using BeginInvoke and EndInvoke ?

http://www.dotnetfunda.com/articles/article807-asynchronous-method-invocation-.aspx[^]


这篇关于在其他线程中运行LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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