Solved

TBitmap from a thread

Posted on 2004-04-01
5
196 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

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

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…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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