Solved

Threads ....

Posted on 2001-06-17
7
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 351
find a node in VST 2 78
Delphi: making a BW image transparent 10 90
Using MMsystem To change audio input 1 15
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

763 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