?
Solved

TBitmap from a thread

Posted on 2004-04-01
5
Medium Priority
?
205 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
[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
  • 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 1000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

771 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