Solved

TBitmap from a thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

912 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

23 Experts available now in Live!

Get 1:1 Help Now