views:

290

answers:

1

I need to put some ListView populate code in a thread. For the simple case, this thread should not run twice. When I want to issue a new thread the previous needs to stop, whatever did.

EDIT 1

The scenario is the following.
I have a textual filter above the ListView. On textchange I call a populateList() method.
The problem is that the code can take longer as it uses SQL LIKE syntax on a larger database.

Until this runs, the user cannot type in nothing. So there are sever hang-outs while you type in "abc", you get to type "c" after 10 seconds only.

So I have in mind to issue the populateList() method in a Thread and allow the user to quickly type in something more. The longer the text the slower takes the SQL query. In the "abc" situation if you type "a" code goes behind and runs the query, but meanwhile if the user pressed "b" I want to stop the execution of the "a" thread, and issue a new one, now using "ab"... and so on with the "c".

EDIT 2

Still looking for more answers.

+1  A: 

I dunno whether i got the question right... If you're dealing with multiple threads and for some reason you want to stop the previous thread, you can check in the new thread whether the previous thread is alive and abandon its action abruptly... If this is not what you expect can you please detail your question?

Update:
Now i understood your question very well. Even I'm working on such an issue but the db being in-memory. In your case, if your db supports LIMIT then you can limit the number of results returned from the query using

LIMIT lowerLimit upperLimit

Now in your thread's method, use a 'for' to loop through the result sets and in each iteration check whether the textbox text has changed. If it was changed then break the loop, clear the list, goto the start of the thread's method and do the same process using the same thread but the textfilter being different.

PSEUDO CODE:

do
  isLoop = false
  clear the collection that is bound to the listbox
  from db, get the record count     
    for: loop 0 to recordcount increment batchsize
      get chuncks of data from db of size batchsize
      add the data to collection [using dispatcher.invoke]
      if textbox text changed
        isLoop = true
        break for-loop
      endif
    endfor
while(isLoop)

Hope this helps.

Veer
see my edit to the question
Pentium10
I will try out this, but I don't know if I will be able to detect the change in the loop, as the event seams to fire only after the population process is done. I will check it out.
Pentium10
Hi Pentium, Just tried it. It works. Let me know if it works for you too. Check my edit for the pseudo code
Veer