Link to home
Start Free TrialLog in
Avatar of rafaelrasche
rafaelrasche

asked on

Delphi Thead Pool or (only run last procedure called)

Hy

I need a way to create a pool, where only the last procedure called must be executed.

I'll explain: Think of a datasource the has a lot of records.
Every record is connected to other child datasources.
Everytime i move to another record, I need to do an HTTP.get (by TIdHttp) from a webservice.

The thing is that it takes a time to retrieve all records.

When the user is moving throug lots of records, my program gets busy, because it  executs all procedures called.

But If the user goes to a second record, and in 300ms, the user changes to other record, I must abort the current process, and leave only the last call running.

If I get this, my program will have a much faster user experience, beause the user won't need to wait for all operations to get concluded.

I made a sample application, to illustrate my question.

It has a dbgrid with records, and a progress bar.
Everytime a move to a new record, it loads the progress bar from 0 to 100, in 10 seconds.

In this time, if I move through 10 diferent records (during, lets say, 3 seconds), it will take 100 seconds to do all the tasks.
But only the last call is important to be executed.

Making this to operate in separeded threads. wont help, because I don't need the data from the other calls. Only from the last one.

Could you give me a tip to accomplish that?

Thank you.
SampleApp.zip
SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rafaelrasche
rafaelrasche

ASKER

I could do it. But it won't solve the problem.
Because, if the get takes 10 seconds to get the data, lets think:
- Imagine the user going to one record. And after 2 seconds, he moves to another record, it will take 2 + 18 seconds to get everything (2x10 seconds).
But, if the current operation could be aborted, it would take only 2 + 10 seconds.

And I'm trying to figure it out a way to do it.
If someone finds a way, I guess it would be useful for lots of things.
Thats the why I thing this is important.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I mentioned it as a simple substitute, geert gave some good advice on a threaded approach. Keep in mind though that it's possible that stuff used by a thread you kill might not be closed properly if you kill it just like that
For Geert_Gruwez:
- Thank you for all the information.
I will begin the tests and, later today, I will tell you if I could reach there or not.

For MerijnB
- You are right. Is was a simple substitute.
But for my need, wasn't the best practice.
I will keep it in mind to avoid memory leak and other stuff.
Probably, I won't create or free anything in this thread.
So, I guess it won't be a problem.
I will be working with things that are already in memory.
Hello Geert_Gruwez.
I've tried to change the TThreadQueue to descends from TObjectStack.
But there are some methods the aren't beeing recognized anymore.
I've tried to fix it, but I couldn't.

Also, I don't know the kill thread procedure.

On my question, I left an attach file with sample app.
http://filedb.experts-exchange.com/incoming/2013/08_w33/672208/SampleApp.zip
It is a very simple one, with few lines.

If you could apply a sample method inside it, I would be very, very thankful.
I don't know if there is a way to give you an extra reward for the work, because you would earn it very much.

Or, give me some tips to get it done.

Thank you.
i'll work out a sample
Thank you very much.
um, this more difficult than i anticipated ... :)
As i was digging into this sample, it seems TerminateThread causes a memory leak
http://caryjensen.blogspot.be/2011/09/creating-tthread-in-delphi-that-can.html

so the approach would have to different.
1: the stack idea will remain
2: once a thread is started, it has to run until the end, to prevent memory leaks
3: when a new thread is started, it should mark the old thread as obsolete
4: only data of a non-obsolete thread should be passed on

reworking my sample ... :)
Ah, the gut feeling did have a reason :)

Something else to keep in mind: is calling the web service a bottleneck? I'm asking because you told before it will take up to a few seconds.

If it is a bottleneck, starting of threads which you cannot terminate might stall the web service (since they are all calling it at once). So if this is the case using the threads might give the user an overall slower experience in stead of faster ;)

Curious how it all will work out!
Hello

Calling the webservice is the bottleneck.
But I was thinking that, before the second call is started, we could kill the thread, and with this, abort the current operation.

So, it wouldn't have two calls at the same time.

But, if I understand right, we can't do that, because of the memory leak.

If there isn't a way to avoid this, I'm afraid that we should take this as not possible.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for helping.
With this information, I'll accomplish the best for user experience.