Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Multithreaded Search with LINQ

Back to Forum: Tech Off
  • User profile image

    I'm not even sure how to pose this question but it has to do with finding out the correct way of doing multi-threaded programming in a windows desktop application. Yes, I know, too big of a topic for a single post.

    Here's what I'm wondering about.

    I have a keywords textbox, a search button, and a list for results. When the user enters a value and clicks the search button, I'm using LINQ to query for results but the application freezes until I'm finished listing the results. Yes Lame!

    So I was thinking that I would kick up a thread to do the query and then return the result back to the primary thread and list the results. Except that LINQ doesn't actually fire off the query until you're using it, like a databound operation.

    So what are my options? <- I apologize for the very non-specific question here.

    Also, in general, if listing out results in a list box takes a long time itself, how are you supposed to multi-thread that operation if the list box was created on the primary application thread.

  • User profile image

    Personally I'm not understanding the problem.. fair enough, linq is an expression, and is stored in a way that its execution is deferred until the actual data is being requested (like enumeration).

    If you create a new thread and do the linq query and binding back to the UI elements then the deferred execution shouldn't really factor in to threading should it?

    Also, isn't the base concept of PLINQ just to add extension methods to LINQ, so that its execution automatically spins itself a new thread etc etc (I understand its more complicated than this in reality)..

    I'm not a big UI + Worker thread guy, I work in web world mostly, but I understand the worker thread can bind back items to the target UI item..


    for each item in linq results

    Obviously theres a level of invocation needed for the worker thread to actually bind to the UI item.. but the way I see it isn't:

    Go get me some data, so I can populate my field.. it should be, here's my field, and populate it with data for me..

    This is of course based upon the abstract in my head that says, this could be a ton of data that you want to 'stream' from an enumeration..

    If it isn't, then I can't see a reason why the worker thread cannot force the execution, and populate a collection with the resulting items, and then hand the collection to the UI thread when done..


    Edit: Just to note, I have barely touched LINQ so far, but it seems theres a method ToList() on the query that will essentially do what I said (start the enumeration and populate a list).

  • User profile image

    The deal here is to fill the listview. That's it. Take LINQ aside and think of how you would fill the listview if you would have the results already in memory...

    Try to find a way to fill it in an asynchroneous way. You could use a backgroundworker or something similar that does it for you...

  • User profile image

    OWW, mybrain hurts. Alright. All very valuable, I need to go dive into the code again. See ya on the flip side!

  • User profile image


    This link helped me out a lot:

    LINQ did need to be taken out of the equation so I could focus on getting a good model for updating the list box with data. Additionally, calling .ToArray off of LINQ worked perfect to get the query to happen on the seperate thread.

    I imagine my work can all be improved but at least I have a frame of reference now. Thanks all!

  • User profile image

    Use asynchronous requests to perform paging LINQ queries (e.g., Skip() and Take()) off cached data and virtualize your listbox to populate with the paged results rather than filling the entire listbox with thousands of results in one time-consuming batch.

    PLINQ and ParallelFx are more useful if the problem you are trying to solve lends itself well to having parcels of work divided among multiple independent processes, like when you are dealing with multiple partitions and-or sets of large data that you want to query (data parallelism). The classic problem example is searching enormous data stores of indexed Web sites to provide Web search like Google does with MapReduce.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.