Solved

TBitmap from a thread

Posted on 2004-04-01
5
199 Views
Last Modified: 2010-04-04
I have a component that is a descendant of a TImage. I use this to play back a custom video format I created.

I have created a thread for my playback loop (both to wait for data over the network before continuing, and for timing). This loop pauses as long as it needs to and then enters a critical section to call a procedure that processes the data and draws it to the canvas.

If I create that loop in the main thread, I don't run into any problems. However when I move that loop the a thread, it will work for a few seconds and then stop drawing. Everythign acts as though it is still working except the data is never displayed.

The loop will be the only thing that calls anything that draws. I tried TCanvas.Lock/Unlock however it just freezes.

Any tips on what to look for?

0
Comment
Question by:rbohac
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10733017

Are you synchronizing the actual data processing/drawing with the main thread? Attempting to interact with the main thread (where the component lives), can cause thread deadlocking and all sorts of other bizarre results.

Regards,
Russell
0
 
LVL 6

Author Comment

by:rbohac
ID: 10733050
I thought I was by entering a critical section first....
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 10733101

A CS enter/leave is good for handling a section of code that should only have one thread in it at a time. But when dealing with components, the component may be accessed by the other thread outside of your section of code. Say for example, a WM_PAINT, or other message gets processed that affects the component. Then both threads are attempting to manipulate the component. Not good...

So no, a CS is not going to cut it when dealing with components (the whole VCL is not thread safe). I would suggest that the actual component loading and image drawing should be done in a synchorinzed method which would then run in the main thread, which is where the component lives.

Regards,
Russell


0
 
LVL 6

Author Comment

by:rbohac
ID: 10733326
Figures it would be something like that. That was the problem. I thought a critical section did the same thing that synchronize did?
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10734252

No, a CS is good for limiting access to a resource (be it a variable, non-vcl object, ect), just as long as all access is done in the CS protected block. With a vcl component, access cannot be protected that way, because the main thread is still pumping the message queue, and may process a message that performs some logic on the resource (component) that you are trying to protect. The only way around it is to perform the logic in the main thread.

Russell


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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