BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using a TThread object in a component dropped on an ActiveFo

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development)
View previous topic :: View next topic  
Author Message
OBones
Guest





PostPosted: Wed Jul 13, 2005 1:01 pm    Post subject: Using a TThread object in a component dropped on an ActiveFo Reply with quote



Hi all

I'm trying to use a TThread object to do some background animation on an
image component. The drawing happens with a call to Synchronize and that
works fine in a regular application.
But when I put the same image component with its TThread, the calls to
Synchronize never work. I've traced it to the sending of the message to
the application, but it seems the message never reaches the ActiveForm.
It's like the host application (made in Delphi as well) eats up this
message.

Could anyone confirm this?

Thanks
Olivier
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Jul 13, 2005 4:52 pm    Post subject: Re: Using a TThread object in a component dropped on an Acti Reply with quote




"OBones" <obones_gfe_ (AT) _gt_altern (DOT) org> wrote


Quote:
I'm trying to use a TThread object to do some background animation
on an image component. The drawing happens with a call to Synchronize
and that works fine in a regular application.

What all does the thread actually do? Unless the thread is also performing
image processing and such, not just calling Synchronize() only, then there
is no point in using a thread at all. You could use a timer instead.

Quote:
But when I put the same image component with its TThread, the calls
to Synchronize never work.

Please show your actual code.


Gambit



Back to top
OBones
Guest





PostPosted: Mon Jul 18, 2005 3:42 pm    Post subject: Re: Using a TThread object in a component dropped on an Acti Reply with quote



Remy Lebeau (TeamB) wrote:

Quote:
"OBones" <obones_gfe_ (AT) _gt_altern (DOT) org> wrote in message
news:42d51025$1 (AT) newsgroups (DOT) borland.com...


I'm trying to use a TThread object to do some background animation
on an image component. The drawing happens with a call to Synchronize
and that works fine in a regular application.

What all does the thread actually do? Unless the thread is also performing
image processing and such, not just calling Synchronize() only, then there
is no point in using a thread at all. You could use a timer instead.

Well timers are a scarce resource, and using a TTimer to simply animate
a GIF seems like a waste of such resources. And the mere calling of
Synchronize does not work. Simple as that. So if I was doing image
processing in it, the progress could not be shown.


Quote:
But when I put the same image component with its TThread, the calls
to Synchronize never work.

Please show your actual code.

TJvTimer in the JVCL3. The code allows to choose between a TTimer or a
TThread, so it works with a TTimer, but I'd really like to understand
what's wrong with the calls to Synchronize. As I said, I suspect the
message to be eaten by the Delphi host application.

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jul 18, 2005 5:36 pm    Post subject: Re: Using a TThread object in a component dropped on an Acti Reply with quote


"OBones" <obones_gfe_ (AT) _gt_altern (DOT) org> wrote


Quote:
using a TTimer to simply animate a GIF seems like a waste of such
resources


A lot less wasteful then spawning a whole new thread that has to delegate
back to the main thread for its work. Spawning a thread is an expensive
operation on the machine, especially if you are not going to actually
utilize the thread's functionality properly.

Quote:
And the mere calling of Synchronize does not work.

Then you are not using it correctly.

Quote:
Simple as that. So if I was doing image processing in it, the
progress could not be shown.

Yes, it can.

Quote:
TJvTimer in the JVCL3. The code allows to choose between a TTimer
or a TThread, so it works with a TTimer, but I'd really like to
understand what's wrong with the calls to Synchronize.

I am not going to download an entire third-party library just to look at
code for one component that you could just post here directly instead.


Gambit



Back to top
OBones
Guest





PostPosted: Tue Jul 19, 2005 6:59 am    Post subject: Re: Using a TThread object in a component dropped on an Acti Reply with quote

Remy Lebeau (TeamB) wrote:

Quote:
TJvTimer in the JVCL3. The code allows to choose between a TTimer
or a TThread, so it works with a TTimer, but I'd really like to
understand what's wrong with the calls to Synchronize.

I am not going to download an entire third-party library just to look at
code for one component that you could just post here directly instead.

Ok, here you go (well, at the end of it)

I've traced it again, the call to synchronize is done, the message is
sent, but the breakpoint inside my handler for OnTimer is never reached.
This leads me to thinking that the message is "eaten" by the host and is
never sent to the active form.
Thanks for your help.

{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the
License for
the specific language governing rights and limitations under the License.

The Original Code is: JvTimer.PAS, released on 2002-07-04.

The Initial Developers of the Original Code are: Fedor Koshevnikov, Igor
Pavluk and Serge Korolev
Copyright (c) 1997, 1998 Fedor Koshevnikov, Igor Pavluk and Serge Korolev
Copyright (c) 2001,2002 SGB Software
All Rights Reserved.

You may retrieve the latest version of this file at the Project JEDI's
JVCL home page,
located at http://jvcl.sourceforge.net

Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvTimer.pas,v 1.20 2005/07/12 16:57:16 obones Exp $

unit JvTimer;

{$I jvcl.inc}

interface

uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
{$IFDEF MSWINDOWS}
Windows, Messages,
{$ENDIF MSWINDOWS}
SysUtils, ExtCtrls, Classes;

type
TJvTimer = class(TComponent)
private
FEnabled: Boolean;
FInterval: Cardinal;
FOnTimer: TNotifyEvent;
FSyncEvent: Boolean;
FThreaded: Boolean;
FTimerThread: TThread;
FTimer: TTimer;
{$IFDEF MSWINDOWS}
FThreadPriority: TThreadPriority;
procedure SetThreadPriority(Value: TThreadPriority);
{$ENDIF MSWINDOWS}
procedure SetThreaded(Value: Boolean);
procedure SetEnabled(Value: Boolean);
procedure SetInterval(Value: Cardinal);
procedure SetOnTimer(Value: TNotifyEvent);
procedure UpdateTimer;
protected
procedure Timer; dynamic;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Synchronize(Method: TThreadMethod);
published
property Enabled: Boolean read FEnabled write SetEnabled default True;
property Interval: Cardinal read FInterval write SetInterval
default 1000;
property SyncEvent: Boolean read FSyncEvent write FSyncEvent
default True;
property Threaded: Boolean read FThreaded write SetThreaded default
True;
{$IFDEF MSWINDOWS}
property ThreadPriority: TThreadPriority read FThreadPriority write
SetThreadPriority default tpNormal;
{$ENDIF MSWINDOWS}
property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer;
end;

{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvTimer.pas,v $';
Revision: '$Revision: 1.20 $';
Date: '$Date: 2005/07/12 16:57:16 $';
LogPath: 'JVCLrun'
);
{$ENDIF UNITVERSIONING}

implementation

uses
Forms, Consts,
JvJVCLUtils;

//=== { TJvTimerThread }
=====================================================

type
TJvTimerThread = class(TThread)
private
FOwner: TJvTimer;
FInterval: Cardinal;
FException: Exception;
procedure HandleException;
protected
procedure Execute; override;
public
constructor Create(Timer: TJvTimer; Enabled: Boolean);
end;

constructor TJvTimerThread.Create(Timer: TJvTimer; Enabled: Boolean);
begin
FOwner := Timer;
inherited Create(not Enabled);
FInterval := 1000;
FreeOnTerminate := True;
end;

procedure TJvTimerThread.HandleException;
begin
if not (FException is EAbort) then
Application.HandleException(Self);
end;

procedure TJvTimerThread.Execute;

function ThreadClosed: Boolean;
begin
Result := Terminated or Application.Terminated or (FOwner = nil);
end;

{$IFDEF UNIX}
function SleepEx(Ms: Cardinal; Alertable: Boolean): Cardinal;
begin
Sleep(Ms);
Result := 0;
end;
{$ENDIF UNIX}

begin
repeat
if (not ThreadClosed) and (SleepEx(FInterval, False) = 0) and
(not ThreadClosed) and FOwner.FEnabled then
with FOwner do
if SyncEvent then
Synchronize(Timer)
else
try
Timer;
except
on E: Exception do
begin
FException := E;
HandleException;
end;
end;
until Terminated;
end;

//=== { TJvTimer }
===========================================================

constructor TJvTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
FInterval := 1000;
FSyncEvent := True;
FThreaded := True;
{$IFDEF MSWINDOWS}
FThreadPriority := tpNormal;
{$ENDIF MSWINDOWS}
FTimerThread := TJvTimerThread.Create(Self, False);
FTimer := nil;
end;

destructor TJvTimer.Destroy;
begin
Destroying;
FEnabled := False;
FOnTimer := nil;
{TTimerThread(FTimerThread).FOwner := nil;}
while FTimerThread.Suspended do
FTimerThread.Resume;
FTimerThread.Terminate;
FTimer.Free;
inherited Destroy;
end;

procedure TJvTimer.UpdateTimer;
begin
if FThreaded then
begin
FreeAndNil(FTimer);
if not FTimerThread.Suspended then
FTimerThread.Suspend;
TJvTimerThread(FTimerThread).FInterval := FInterval;
if (FInterval <> 0) and FEnabled and Assigned(FOnTimer) then
begin
{$IFDEF MSWINDOWS}
FTimerThread.Priority := FThreadPriority;
{$ENDIF MSWINDOWS}
while FTimerThread.Suspended do
FTimerThread.Resume;
end;
end
else
begin
if not FTimerThread.Suspended then
FTimerThread.Suspend;
if not Assigned(FTimer) then
FTimer := TTimer.Create(Self);
FTimer.Interval := FInterval;
FTimer.OnTimer := FOnTimer;
FTimer.Enabled := (FInterval <> 0) and FEnabled and Assigned(FOnTimer);
end;
end;

procedure TJvTimer.SetEnabled(Value: Boolean);
begin
if Value <> FEnabled then
begin
FEnabled := Value;
UpdateTimer;
end;
end;

procedure TJvTimer.SetInterval(Value: Cardinal);
begin
if Value <> FInterval then
begin
FInterval := Value;
UpdateTimer;
end;
end;

procedure TJvTimer.SetThreaded(Value: Boolean);
begin
if Value <> FThreaded then
begin
FThreaded := Value;
UpdateTimer;
end;
end;

{$IFDEF MSWINDOWS}
procedure TJvTimer.SetThreadPriority(Value: TThreadPriority);
begin
if Value <> FThreadPriority then
begin
FThreadPriority := Value;
if FThreaded then
UpdateTimer;
end;
end;
{$ENDIF MSWINDOWS}

procedure TJvTimer.Synchronize(Method: TThreadMethod);
begin
if FTimerThread <> nil then
begin
with TJvTimerThread(FTimerThread) do
begin
if Suspended or Terminated then
Method
else
TJvTimerThread(FTimerThread).Synchronize(Method);
end;
end
else
Method;
end;

procedure TJvTimer.SetOnTimer(Value: TNotifyEvent);
begin
if Assigned(FOnTimer) <> Assigned(Value) then
begin
FOnTimer := Value;
UpdateTimer;
end
else
FOnTimer := Value;
end;

procedure TJvTimer.Timer;
begin
if FEnabled and not (csDestroying in ComponentState) and
Assigned(FOnTimer) then
FOnTimer(Self);
end;

{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);

finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Development) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.