Solved

Threads ....

Posted on 2001-06-17
7
261 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
Comment Utility
Why don't you use 'Synchronize'?
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:asi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

17 Experts available now in Live!

Get 1:1 Help Now