Link to home
Start Free TrialLog in
Avatar of lizzzard
lizzzardFlag for Netherlands

asked on

Thread question.

Hi,

I'm making an application that's making some reports from a FoxPro database (Query). Five separate statistics are involved. I want my user to be able to hit the Cancel button while processing the data, so I guess I need a thread here.
My question is twofold:
A) Can I (using threads) process the 5 processes at the same time?
B) How can I kill all threads at work to stop when my user hits the Cancel button.

What I have now is one thread, which defines an array of five threads, each processing another statistic. However, I have to WaitFor all these threads to end before I can show the result. When I use the WaitFor procedure, the application won't react on my Cancel button...

Can anybody give me an example of how I can do this?

Thanks!
Avatar of Lischke
Lischke

Hi lizzzard,

I think you are on the right track. But I recommend to use the WinAPI WaitForMultipleObjects and give it a timeout of, say, 1 second to give back control to the application. Another way would be to define 5 events which are set each by the corresponding thread when it has finished its report. The "supervisor" thread can what for all the  events to be signalled before returning to the application while the app. still can respond to UI events and eventually cancel the threads.

Ciao, Mike
Oops, typos: The "supervisor" thread can wait for all the events...
Avatar of lizzzard

ASKER

Thank again Mike..

One more question...
If I kill a thread, does it allways kill it's child-threads?
No, when a thread finishes then spawned threads keep alive as they are in reality part of the process they run in. But if the process is stopped then all its threads are also stopped. If the thread being stopped is the last one in a process then the process is also stopped. This is because a process is just a thread with some addition properties.

Ciao, Mike
Hi,
first of all, do you want to kill them or end them. If you want to end them, setup your remote threads and lower their priority. Otherwise they may block your main thread waiting for the Cancel. The best way in my opinion is to have a special event set, when a cancel occurs. Inside each worker thread there are frequent calls to that event like: if WaitForSingleObject (killEvent,0) = WAIT_OBJECT_0 then exit;
This will bring your code the safe way out and you can clean up correct. Especially with databases this behaviour is proposed.
To guarantee that your main will not stop before all threads are gone, you post in the OnTerminate of each thread a message that decrements the count of running threads. if all are gone a flag is set, that allows to stop main.

This is working very well for me and has proved to be stable, especially with databases.

hope i could help you
NixNoxe,

When the user presses the Cancelbutton, I just want each thread processing a statistic to stop. Could you please work out a small example for the cancel event using the waitforsingleobject? I can't really figure out what to do.

Thanks,

Lizzzard

ASKER CERTIFIED SOLUTION
Avatar of niknoxe
niknoxe

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
listening...
If you have further questions (there are some tricky things right in front of you), please contact me directly: niknoxe@hotmail.com.

best regards
Thanks!

I'm sure you'l hear from me...

Regards,

Lizzzard