Link to home
Start Free TrialLog in
Avatar of asi
asi

asked on

Threads ....

My Application has about 8 thereads that need to update
Grid very often.

It happend that the application stop respond.
I guess that this is because threads problem

I use every grid update with      EnterCriticalSection(CriticalSec);

My quesiton how can i detect the bug ?



Avatar of Epsylon
Epsylon

Why don't you use 'Synchronize'?
Are you utilizing the Sychronize as: Epsylon  has suggested.  You must utilize this in order to access the VCL from a thread. Further more, 8 threads plus the initial process which is a thread itself, makes nine threads. You must make sure you have the memory resource to have that many threads. Try disabling all threads and running one at a time until you find the thread with the problem. However, I am sure the problem is in the Synchronize by the brief spec you have posted.


Shane Holmes
Using Synchronize is only a choice if updating the grid is only a minor task of the threads. If updating the grid is the main task of the threads, then forget about Synchronize. Synchronize is a hack, that makes the main thread execute the synchronized method.

However, as already indicated by the other experts, the VCL is not thread safe. So basically you must not access VCL components in other threads than the main thread.

A solution could be to use pure win32 APIs/controls instead of VCL components. These are thread safe and so can be accessed safely by all the threads.

If you want to know why your program stopped responding exactly, I have a trick for you: Just last week I've written a little utility which hooks into a hanging/freezed Delphi application and shows the current callstack of all threads. This way you can e.g. see, that thread 2 and 3 are waiting for the critical section or something like that. In order for this utility to work, your application must have mapfiles attached with my package "madExcept". It's free for non-commercial usage and also free for any usage on your development PC. If you're interested give me your eMail address, then I'll send the utility to you. It runs under NT/2000 only, though, not under win9x...

Regards, Madshi.

http://help.madshi.net/Data/madExcept.htm
Avatar of asi

ASKER

madshi
b_asi@hotmail.com

The problem is that my application only freeze and i do not get any exception , does your utility will help ?
As Madshi said 'Synchronize is a hack', Only last friday I had a program that was using Synchronize to run a VCL method.  But the program wasn't working.

I fixed the problem by posting a message to the form (that contains the Object) then waiting for the form to set a flag, before contining, also using the Sleep procedure to put the thread to sleep and allow the other threads to use the CPU.  This works extremly well, and I don't think that I'll bother with the Syncronize method ever again!

Colin Dawson
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

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, basicaly, like the EnterCriticalSection(); approach. It is much better than Synchronize, because it is in Windows' nature. However, a deadlock may easily occure. If you share some of your code, or at least the idea you use I may be able to help you.
  And to Shane Holmes: it is not a big overhead for Windows to hold up 9 more threads: this is what it is made for. The thread structure is not a big resource/memory user. And switching between threads is not big deal for Windows - change the state register, the stack and few other pointers and poof! - you are done. It would be much worse if it was 9 processes.