Solved

Threads ....

Posted on 2001-06-17
7
262 Views
Last Modified: 2010-04-06
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 ?



0
Comment
Question by:asi
7 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6200177
Why don't you use 'Synchronize'?
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 6200557
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
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6201075
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:asi
ID: 6201610
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 ?
0
 
LVL 4

Expert Comment

by:Colin_Dawson
ID: 6201676
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
0
 
LVL 20

Accepted Solution

by:
Madshi earned 150 total points
ID: 6201705
Yes, asi, to analyze a frozen process (without a raised exception) is exactly what I've written this utility for.

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6201774
 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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
automatically logging in via bde delphi 3 68
Best Firemonkey component pack 1 85
Virtuailstring tree compare node issue 14 103
Delphi Form ownership 4 70
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now