Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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


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

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
Posted

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.
 
Share this answer
 
Comments
lxmyers 15-Sep-10 0:47am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Do you know delegates are capable of creating threads implicitely using BeginInvoke and EndInvoke ?

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900