Board index » delphi » Threads and the VCL, calling Paint from 2 Threads

Threads and the VCL, calling Paint from 2 Threads

Hi,

I'm no beginner, but it's the first time I use threads.

*** The senario :

A 3d button component should blink (Flash) steady even when the main thread
is busy.

*** How :

I create a blink Thread and a Critical Section in the class Initialization
like this :

Initialization
  BusyPainting:=TCriticalSection.Create;
  BlinkThread:=TBlinkThread.Create(False);
----
I use the critical section to protect PAINT so only one of the threads can
perform the painting at a given time,
Using BlinkTread.Enter at the start and BlinkThread.Leave at the end of my
Paint procedure.

When the Blinkthread finds that it's time for the button(s) to blink it
calls the code which I don't get right, for each created button. (Using a
TList)

*** What I tried :

Calling Paint directly or  "  BeginPaint(Handle, PS);  Paint;
EndPaint(Handle, PS); " :

 The Buttons blinks, but there is a lot of problems when the main thread
performs other (VCL ?)  actions at the same time, like wrong colors, lines
wrong places, error messages (mainly something about cavas dont allow
painting) and Self.Focused returns wrong results. (Returns false when
focused)

Repaint or Invalidate:

Wait's for the main thread witch is not what I wanted, but perform the task
with no problems.

I also tried to use perform or sendmessage, but it has no function. Maybe I
use it wrong. :

    MemDC := CreateCompatibleDC(0);
    Perform(WM_Paint,MemDC,MemDC);
//    SendMessage(Handle,WM_Paint,MemDC,0);
     DeleteDC(MemDC);

****************************************************************************
*****

Anyone ? This is driving me nuts.

Regards from Norway,  Tom-Vidar Nilsen

 

Re:Threads and the VCL, calling Paint from 2 Threads


Quote
Tom-Vidar Nilsen wrote:

> Hi,

> I'm no beginner, but it's the first time I use threads.

> *** The senario :

> A 3d button component should blink (Flash) steady even when the main thread
> is busy.

The usual scenario is to do the "real work" in a thread, leaving the
main thread free to process Windows messages, and if necessary, to blink
the button using an ordinary timer.  

In other words, instead of spinning off a thread to do the blinking,
while the main thread runs (and monopolizes the Windows queue, which is
the problem...), spin off a thread to do the work and let the main
thread handle the blink.

Re:Threads and the VCL, calling Paint from 2 Threads


Quote
> > A 3d button component should blink (Flash) steady even when the main
thread
> > is busy.

> The usual scenario is to do the "real work" in a thread, leaving the
> main thread free to process Windows messages, and if necessary, to blink
> the button using an ordinary timer.

> In other words, instead of spinning off a thread to do the blinking,
> while the main thread runs (and monopolizes the Windows queue, which is
> the problem...), spin off a thread to do the work and let the main
> thread handle the blink.

Sure, but how can I do this when I create a component.
The user of the component shouldn't have to know anything about this, that's
the whole idea.

What I'm searching for is realy a safe way to FORCE a paint directly,
without waithing messages.
If it's not possible to use the VCL for this, even when I prevent
reenterance in Paint, then I may have to do it without the VCL, but that
would be hard to make.

Do anyone know if I can find something simular to bmp.canvas.scanline
without VCL ?

Regards

Other Threads