What is the best solution to show that the application is doing something?
I tried showing a progress indicator, but it did not work.
UPDATE: -------------
A progress bar works fine, but isn't what I want.
I want to show a throbber, like what Web browsers use, so as long as something is being updated it keeps turning.
Cursor can also be in crHourGlass mode.
Try this:
AnimateUnit
unit AnimateUnit;
interface
uses
Windows, Classes;
type
TFrameProc = procedure(const theFrame: ShortInt) of object;
TFrameThread = class(TThread)
private
{ Private declarations }
FFrameProc: TFrameProc;
FFrameValue: ShortInt;
procedure SynchedFrame();
protected
{ Protected declarations }
procedure Frame(const theFrame: ShortInt); virtual;
public
{ Public declarations }
constructor Create(theFrameProc: TFrameProc; CreateSuspended: Boolean = False); reintroduce; virtual;
end;
TAnimateThread = class(TFrameThread)
private
{ Private declarations }
protected
{ Protected declarations }
procedure Execute(); override;
public
{ Public declarations }
end;
var
AnimateThread: TAnimateThread;
implementation
{ TFrameThread }
constructor TFrameThread.Create(theFrameProc: TFrameProc; CreateSuspended: Boolean = False);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
FFrameProc := theFrameProc;
end;
procedure TFrameThread.SynchedFrame();
begin
if Assigned(FFrameProc) then FFrameProc(FFrameValue);
end;
procedure TFrameThread.Frame(const theFrame: ShortInt);
begin
FFrameValue := theFrame;
try
Sleep(0);
finally
Synchronize(SynchedFrame);
end;
end;
{ TAnimateThread }
procedure TAnimateThread.Execute();
var
I: ShortInt;
begin
while (not Self.Terminated) do
begin
Frame(0);
for I := 1 to 8 do
begin
if (not Self.Terminated) then
begin
Sleep(120);
Frame(I);
end;
end;
Frame(0);
end;
end;
end.
Unit1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ImgList;
type
TForm1 = class(TForm)
ImageList1: TImageList;
Image1: TImage;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure UpdateFrame(const theFrame: ShortInt);
end;
var
Form1: TForm1;
implementation
uses
AnimateUnit;
{$R *.DFM}
procedure TForm1.UpdateFrame(const theFrame: ShortInt);
begin
Image1.Picture.Bitmap.Handle := 0;
try
ImageList1.GetBitmap(theFrame, Image1.Picture.Bitmap);
finally
Image1.Update();
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AnimateThread := TAnimateThread.Create(UpdateFrame);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
AnimateThread.Terminate();
end;
end.
The Images
You are probably running your time consuming task in the main thread.
One option is to move it to a background thread which will allow your message queue to be serviced. You need it to be serviced in order for your progress bar, and indeed any UI, to work.
Answer to the updated question:
generate an animated gif e.g. here
add a GIF library to your environment (JEDI JVCL+JCL)
insert a TImage and load the generated gif
make it visible if you need it
A indicator is OK. You have to call Application.ProcessMessages after changing it.
"What is the best solution to show that that application is doing something?" - set mouse cursor to crHourGlass? or to create another form/frame/etc which attentions the user that the application is 'doing' something, and he needs to wait.
From your lengthy task, you can occasionally update a visual indicator, like a progress bar or anything else. However, you need to redraw the changes immediately by calling Update on the control that provides the feedback.
Don't use Application.ProcessMessages as this will introduce possible reentrancy issues.
Related
I have tested my own class with the dependency injection and now I have to implement it into production. The following is an excerpt of my class and relevant interface:
ITableDB = interface
['{171DE959-8604-4CD3-ACEA-ACCE15E95621}']
procedure Close;
procedure Open;
...
end;
TNewStrategy=class(TObject)
private
FTableDB: ITableDB
.....
public
constructor Create (ATableDB: ITableDB....)
end;
Instead of mocks and stubs I have to provide the class the real objects now. These are a number of third part components I have placed in a form at design time. Here one example:
type
TForm1 = class(TForm)
ThirdyPartDBTable1: ThirdyPartDBTable;
NewStrategy: TNewStrategy;
private
{ Private declarations }
public
{ Public declarations }
end;
How can I pass ThirdyPartDBTable1 to TNewStrategy.Create ? I tried the following code:
TMyThirdyPartDBTable = class(ThirdyPartDBTable, IITableDB)
public
procedure Close;
procedure Open;
...
end;
But when I try to change ThirdyPartDBTable1: ThirdyPartDBTable into ThirdyPartDBTable1: TMYhirdyPartDBTable; the compiler changes the reference TMYhirdyPartDBTable back to ThirdyPartDBTable.
In the code you showed, TNewStrategy is not derived from TComponent, so it cannot be placed on a TForm at design-time. You would have to create it at run-time, in which case you have access to its constructor and can pass ThirdyPartDBTable1 to it, eg:
procedure TForm1.FormCreate(Sender: TObject);
begin
NewStrategy := TNewStrategy.Create(ThirdyPartDBTable1);
end;
However, if TNewStrategy were a TComponent descendant available at design-time, you could link ThirdyPartDBTable1 to NewStrategy at design-time if you change TNewStrategy to expose an ITableDB property instead of passing it in the constructor, eg:
TNewStrategy = class(TComponent)
private
FTableDB: ITableDB
.....
public
constructor Create(AOwner: TComponent); override;
published
property TableDB: ITableDB read FTableDB write FTableDB;
end;
As long as ThirdyPartDBTable implements ITableDB then the Object Inspector and DFM streaming will allow it.
Update: since ThirdPartyDBTable does not implement ITableDB, you can use an interceptor class to implement it, eg:
interface
uses
..., ThirdPartyUnit;
type
ThirdyPartDBTable = class(ThirdPartyUnit.ThirdyPartDBTable, ITableDB)
public
procedure Close;
procedure Open;
end;
TForm1 = class(TForm)
ThirdyPartDBTable1: ThirdyPartDBTable;
NewStrategy: TNewStrategy;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
procedure ThirdyPartDBTable.Close;
begin
...
end;
procedure ThirdyPartDBTable.Open;
begin
...
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
NewStrategy := TNewStrategy.Create(ThirdyPartDBTable1 as ITableDB);
end;
end.
You cannot change the class of a component that you've put at design time by modifying it in the form declaration, the IDE owns the declarations in the upper public part of the form.
You can create your derived component at run time instead, or install it in a run time package and register with the component library. For a single time job, or for testing purposes, you can use an interposer class. In the below example I used a TPanel since I don't have any ThirdyPartDBTable, so be sure to put a panel on the test form. Also omitted the 'Close' method for brevity.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
ITableDB = interface
['{171DE959-8604-4CD3-ACEA-ACCE15E95621}']
procedure Open;
end;
TPanel = class(extctrls.TPanel, ITableDB)
public
procedure Open;
end;
TNewStrategy=class(TObject)
private
FTableDB: ITableDB;
public
constructor Create(ATableDB: ITableDB);
end;
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
NewStrategy: TNewStrategy;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TPanel }
procedure TPanel.Open;
begin
ShowMessage('Open what?');
end;
{ TNewStrategy }
constructor TNewStrategy.Create(ATableDB: ITableDB);
begin
FTableDB := ATableDB;
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
NewStrategy := TNewStrategy.Create(Panel1 as ITableDB);
NewStrategy.FTableDB.Open;
end;
end.
I created my own component (it like a button which can move) in Delphi, installed it. Then I create a new project and new from there and added few new my component elements. But only last one added is able to move! Others not. Why does it happen? How could I fix it?
Here's the component code:
unit ModifiedButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
var Timer: TTimer;
type
TSpeed = (Slow,Normal,Fast);
TModifiedButton = class(TButton)
private
{ Private declarations }
FCount:integer;
Velocity:integer;
FSpeed:TSpeed;
protected
{ Protected declarations }
procedure Click;override;
procedure Move(Vel:Integer);
procedure OnTimer(Sender: TObject);
public
{ Public declarations }
procedure ShowCount;
published
{ Published declarations }
property Count:integer read FCount write FCount;
property Speed: TSpeed read FSpeed write FSpeed;
constructor Create(aowner:Tcomponent); override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MyComponents', [TModifiedButton]);
end;
{ TModifiedButton }
procedure TModifiedButton.Click;
begin
inherited Click;
FCount:=FCount+1;
if (Speed = Slow) then
Velocity:=2;
if (Speed = Normal) then
Velocity:=10;
if (Speed = Fast) then
Velocity:= 20;
Timer.Enabled:=True;
end;
constructor TModifiedButton.Create(aowner: Tcomponent);
begin
inherited Create(aowner);
Timer:=TTimer.Create(self);
Timer.Enabled:=false;
Timer.OnTimer:=OnTimer;
Timer.Interval:=10;
end;
procedure TModifiedButton.Move(Vel: Integer);
begin
Left:=Left + Vel;
end;
procedure TModifiedButton.OnTimer(Sender: TObject);
begin
Move(Velocity);
end;
procedure TModifiedButton.ShowCount;
begin
ShowMessage('You cliked '+ caption+' for '+inttostr(FCount)+' times');
end;
end
.
Since the Timer is a global variable, each new button you create will overwrite the OnTimer event handler of the previous button. Solution, make the Timer a member of your TModifiedButton class:
unit ModifiedButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TSpeed = (Slow,Normal,Fast);
TModifiedButton = class(TButton)
private
{ Private declarations }
FCount:integer;
Velocity:integer;
FSpeed:TSpeed;
Timer: TTimer;
protected
{ Protected declarations }
procedure Click;override;
procedure Move(Vel:Integer);
procedure OnTimer(Sender: TObject);
public
{ Public declarations }
procedure ShowCount;
published
{ Published declarations }
property Count:integer read FCount write FCount;
property Speed: TSpeed read FSpeed write FSpeed;
constructor Create(aowner:Tcomponent); override;
destructor Destroy; override;
end;
procedure Register;
implementation
{ TModifiedButton }
procedure TModifiedButton.Click;
begin
inherited Click;
FCount:=FCount+1;
Case Speed of
Slow : Velocity:=2;
Normal : Velocity:=10;
Fast : Velocity:= 20;
end;
Timer.Enabled:=True;
end;
procedure TModifiedButton.Move(Vel: Integer);
begin
Left:=Left + Vel;
end;
procedure TModifiedButton.OnTimer(Sender: TObject);
begin
Move(Velocity);
end;
procedure TModifiedButton.ShowCount;
begin
ShowMessage(Format('You clicked %s for %d times', [Caption, FCount]));
end;
constructor TModifiedButton.Create(AOwner: Tcomponent);
begin
inherited Create(AOwner);
Timer := TTimer.Create(self);
Timer.Enabled:=false;
Timer.OnTimer:=OnTimer;
Timer.Interval:=10;
end;
destructor Destroy;
begin
Timer.Enabled := False;
Timer.Free;
end;
procedure Register;
begin
RegisterComponents('MyComponents', [TModifiedButton]);
end;
end.
I have a code here:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
IInnerTest = interface (IInterface)
procedure DoSth;
end;
TRekScannerData = record
Source: Integer;
Device: IInnerTest;
end;
ITest = interface (IInterface)
procedure DoSth;
end;
ATest = class(TInterfacedObject, ITest)
private
FInner: Array of TRekScannerData;
public
procedure DoSth;
constructor Create();
Destructor Destroy();override;
end;
AInnerTest = class (TInterfacedObject, IInnerTest)
private
FMainInt: ITest;
public
constructor Create(MainInt: ITest);
procedure DoSth;
Destructor Destroy();override;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
test: ITest;
implementation
{$R *.dfm}
{ ATest }
constructor ATest.Create;
begin
SetLength(FInner, 1);
FInner[0].Device := AInnerTest.Create(self);
//<----- Here is the reason. Passing main interface to the inner interface.
end;
destructor ATest.Destroy;
begin
beep;
inherited;
end;
procedure ATest.DoSth;
begin
//
end;
{ AInnerTest }
constructor AInnerTest.Create(MainInt: ITest);
begin
FMainInt := MainInt;
end;
destructor AInnerTest.Destroy;
begin
beep;
inherited;
end;
procedure AInnerTest.DoSth;
begin
//
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
test := ATest.Create;
test.DoSth;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
test := nil;
end;
end.
The problem is that Destroy is not called when test is assigned to nil;
I would like to release all the inner interfaces by one statement ...
Is it possible? or do I need to prior to nil destroy all inner structures by using another method?
EDIT
The class structure is as follows:
Var x = ITest(ATest class) has ->
Inner Interface: IInnerTest(AInnerTest class) which has reference to:
ITest(ATest class)
Nil'ing x doesn't release all structure ...
You have a circular reference. Your implementation of IInnerTest holds a reference to ITest. And your implementation of ITest holds a reference to IInnerTest. And this circular reference means that the interface reference count can never go to zero.
The normal solution to this issue to to use a weak reference. Some useful links:
"Weak reference": down to earth explanation needed
http://www.finalbuilder.com/Resources/Blogs/PostId/410/WeakRefence-in-Delphi-solving-circular-interfac.aspx
http://delphisorcery.blogspot.co.uk/2012/06/weak-interface-references.html
Is it possible using the standard TTreeView to change the Expand and Collapse Image?
I don't mean Node images, I mean the little arrows next to Nodes that have children, like so:
Ideally I would like the arrows to show as + and - Symbols, like the Delphi component structure tree:
If it is possible to change this, how would I go about doing it?
Working Demo based on David's Answer
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, Themes, uxTheme;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TMyTreeView = class(TTreeView)
protected
procedure CreateWnd; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyTreeView }
procedure TMyTreeView.CreateWnd;
begin
inherited;
if ThemeServices.Enabled and CheckWin32Version(6, 0) then
SetWindowTheme(Handle, nil, nil);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
MyTree: TMyTreeView;
Node: TTreeNode;
begin
MyTree := TMyTreeView.Create(nil);
with MyTree do
begin
Parent := Self;
Height := 100;
Width := 100;
Left := 30;
Top := 30;
Node := Items.Add(nil, 'Item');
Items.AddChild(Node, 'Item');
Node := Items.AddChild(Node, 'Item');
Items.AddChild(Node, 'Item');
end;
end;
end.
The Result:
Tree views in post-Vista Windows have two alternative themes. The theme that you are wanting to avoid is known as the explorer theme. You want to use the standard theme. A control has to opt-in to get the explorer theme. It does so via the SetWindowTheme API. The VCL tree view control calls this to opt-in. It does so at the end of its CreateWnd method.
You can revert to the standard theme by undoing the change like this:
type
TMyTreeView = class(TTreeView)
protected
procedure CreateWnd; override;
end;
procedure TMyTreeView.CreateWnd;
begin
inherited;
if StyleServices.Enabled and TOSVersion.Check(6) and StyleServices.IsSystemStyle then
SetWindowTheme(Handle, nil, nil);
end;
This code is written for XE2. If you have an earlier Delphi then I think you want it like this:
if ThemeServices.Enabled and CheckWin32Version(6, 0) then
SetWindowTheme(Handle, nil, nil);
I addition to Davids answer. Put the following code in some extra unit and add it in the uses after the ComCtrls unit. That way you can use the standard TTreeView and change the theme whenever you like. Or register it in your own package if you like.
type
TTreeView = class(ComCtrls.TTreeView)
private
procedure SetExplorerTheme(const Value: Boolean);
public
property ExplorerTheme: Boolean write SetExplorerTheme;
end;
procedure TTreeView.SetExplorerTheme(const Value: Boolean);
begin
if ThemeServices.ThemesEnabled and CheckWin32Version(6, 0) then
if Value then
SetWindowTheme(Handle, 'Explorer', nil)
else
SetWindowTheme(Handle, nil, nil);
end;
In never Delphi versions you could also use a class helper to avoid the extra inheritance.
Summarization:
1. Manual typecast when debugging, as LachlanG and Ken pointed out.
2. Make use of the concept of Debugger Visualizers introduced since Delphi 2010.
3. Switch to generics counterparts.
=========================================
Take the following code for example:
If breakpoints are set at the end of TestRegular, and at the end of TestGenerics, respectively, one can see the items of the generic list(and even the content of the items) through the debug inspector, but nothing meaningful (not even the count) for the regular tobjectlist, when one hovers the mouse on the tmp variable. I am wondering if there is some way to achieve similar debug-time functionality for regular tobjectlist?
unit Unit2;
interface
uses
Contnrs, Generics.Collections,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TMyItem = class;
TMyItemList = class;
TForm2 = class;
TMyItem = class
private
fname: string;
public
property name: string read fname;
constructor Create(aName: string);
end;
TMyItemList = class(TObjectList)
protected
procedure SetObject (Index: Integer; Item: TMyItem);
function GetObject (Index: Integer): TMyItem;
public
function Add (Obj: TMyItem): Integer;
procedure Insert (Index: Integer; Obj: TMyItem);
property Objects [Index: Integer]: TMyItem
read GetObject write SetObject; default;
end;
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure TestRegular;
procedure TestGenerics;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TMyItem }
constructor TMyItem.Create(aName: string);
begin
fname := aName;
end;
{ TMyItemList }
function TMyItemList.Add(Obj: TMyItem): Integer;
begin
Result := inherited Add (Obj);
end;
procedure TMyItemList.SetObject(Index: Integer; Item: TMyItem);
begin
inherited SetItem (Index, Item);
end;
function TMyItemList.GetObject(Index: Integer): TMyItem;
begin
Result := inherited GetItem (Index) as TMyItem;
end;
procedure TMyItemList.Insert(Index: Integer; Obj: TMyItem);
begin
inherited Insert(Index, Obj);
end;
{TForm2}
procedure TForm2.FormCreate(Sender: TObject);
begin
TestGenerics;
TestRegular;
end;
procedure TForm2.TestRegular;
var
tmp: TMyItemList;
begin
tmp := TMyItemList.Create;
tmp.Add(TMyItem.Create('1'));
tmp.Add(TMyItem.Create('2'));
tmp.Free;
end;
procedure TForm2.TestGenerics;
var
tmp: TObjectList<TMyItem>;
begin
tmp := TObjectList<TMyItem>.Create;
tmp.Add(TMyItem.Create('1'));
tmp.Add(TMyItem.Create('2'));
tmp.Free;
end;
end.
I don't think you'll be able to improve what appear in the mouse cursor hover hint.
You can however use typecasts inside Debug windows just as you can within source code.
For example you could typecast the tmp variable to TObjectList(tmp) from within the Evaluation Window (Ctrl F7) or create a Watch (Ctrl F5) on the typecasted variable.
There are Debugger Visualizers that allow you to customise the debugger's visualization capabilities. I've never used them, but it is my understanding that you could combine them with some RTTI and give richer information about a TObject instance.
However, using generics is what you want here. It gives compile time typing which has manifest advantages. I'd simply do it that way.