How can free Interface implemented class? - delphi

I have a little problem. As the title says I want to release an object whose class implements an interface, however I get an error "invalid pointer operation".
My Interface:
Interface
Type
// An interface definition
IMessageEvents = Interface(IInterface)
['{BD27EFC6-CC9A-437A-A8B8-16F722518836}']
Procedure messageReceived(messageData: String);
End;
Implementation
My Class:
Type
TChatManager = Class(TInterfacedObject, IMessageEvents)
Private
Protected
Procedure messageReceived(messageData: String); Overload;
Public
Constructor Create; Overload;
Destructor Destroy; Override;
Procedure connect;
Procedure disconnect;
End;
Implementation
Constructor TChatManager.Create;
Begin
{ ....... }
End;
Procedure TChatManager.connect;
Begin
{ ....... }
End;
Procedure TChatManager.disconnect;
Begin
{ ....... }
End;
Procedure TChatManager.messageReceived(messageData: String);
Begin
{ ....... }
End;
Destructor TChatManager.Destroy;
Begin
Inherited Destroy;
End;
My Code:
self.chatMng := TChatManager.Create;
self.chatMng.Free;
Can anyone tell me what I'm doing wrong? Thanks in advance.

It would appear that chatMng is of type TChatManager. That can be deduced by the fact that you assign TChatManager.Create to it, and call Free on it.
However, TChatManager derives from TInterfacedObject. That means that its lifetime is controlled by the references that are taken to its interfaces. When the final reference is released, the object is destroyed.
The rule of thumb is that if you derive from TInterfacedObject then you must never take a reference to the object other than through an interface variable.
Steps to correct your code:
Change chatMng to be of type IMessageEvents.
Remove the call to chatMng.Free which the compiler will object to in any case.

He must not be showing us all the code...because what he shows should not cause a GPF...you can create a TInterfacedObject and free it...if you don't get a reference to it...but If you get a Reference you are no longer in charge of freeing it...
Here's the Interface
unit Unit3;
Interface
uses
Classes, SysUtils;
Type
// An interface definition
IMessageEvents = Interface(IInterface)
['{BD27EFC6-CC9A-437A-A8B8-16F722518836}']
Procedure messageReceived(messageData: String);
End;
Type
TChatManager = Class(TInterfacedObject, IMessageEvents)
Private
FStrings: TStrings;
Protected
Procedure messageReceived(messageData: String); Overload;
procedure UpdateStatus(aString: string);
Public
Constructor Create(aStrings: TStrings);
Destructor Destroy; Override;
Procedure connect;
Procedure disconnect;
End;
Implementation
Constructor TChatManager.Create(aStrings: TStrings);
Begin
{ ....... }
FStrings := aStrings;
UpdateStatus('Created');
Connect;
End;
Procedure TChatManager.connect;
Begin
{ ....... }
UpdateStatus('Connected');
End;
Procedure TChatManager.disconnect;
Begin
{ ....... }
UpdateStatus('DisConnected');
End;
Procedure TChatManager.messageReceived(messageData: String);
Begin
{ ....... }
UpdateStatus('Message Received');
UpdateStatus(messageData);
End;
procedure TChatManager.UpdateStatus(aString: string);
begin
FStrings.Add(aString);
FStrings.Add('RefCount: '+ IntToStr(Self.RefCount));
end;
Destructor TChatManager.Destroy;
Begin
Disconnect;
UpdateStatus('Destroyed');
Inherited Destroy;
End;
end.
Here's the form
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, unit3, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
chatMng: TChatManager;
iChatMng: IMessageEvents;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
type
THackManager= class(TChatManager);
procedure TForm2.Button1Click(Sender: TObject);
begin
chatMng := TChatManager.Create(Memo1.Lines);
THackManager(ChatMng).messageReceived('Hello World from Button1');
chatMng.Free;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
iChatMng := TChatManager.Create(Memo1.Lines);
iChatMng.messageReceived('Hello World from Button2');
iChatMng := nil;
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
ChatMng := TChatManager.Create(Memo1.Lines);
(ChatMng as IMessageEvents).messageReceived('Hello World from Button3');
//you can't call ChatMng...it's gone bye bye...
//ChatMng.Free; //this will cause a GPF if you call free
end;
procedure TForm2.Button4Click(Sender: TObject);
var
a_IChatMng: IMessageEvents;
begin
ChatMng := TChatManager.Create(Memo1.Lines);
a_IChatMng := chatMng;
(ChatMng as IMessageEvents).messageReceived('Hello World from Button4');
a_IChatMng.messageReceived('Hello World again from Button4');
//ChatMng.Free; //this will cause a GPF if you call free
end;
end.
Here's the dfm
object Form2: TForm2
Left = 326
Top = 94
Caption = 'Form2'
ClientHeight = 292
ClientWidth = 581
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 40
Top = 200
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 40
Top = 32
Width = 411
Height = 129
TabOrder = 1
end
object Button2: TButton
Left = 160
Top = 200
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
object Button3: TButton
Left = 272
Top = 200
Width = 75
Height = 25
Caption = 'Button3'
TabOrder = 3
OnClick = Button3Click
end
object Button4: TButton
Left = 376
Top = 200
Width = 75
Height = 25
Caption = 'Button4'
TabOrder = 4
OnClick = Button4Click
end
end

Related

Form creates 2 Frames - How to call procedure in Frame 2 from inside Frame 1?

Right now Frame 1 is in a loop (looking for data from Serial Comport) and writes to a string variable A in a separate unit. Frame1 then loops until another boolean variable B is true meaning Frame2 has processed its routine.
Frame 2 uses a timer to check for changes in variable A then executes a procedure when the variable has changed and sets boolean variable B to true.
Looping in Frame 1 and checking for variable B to become true leads to Frame 2 can't fire it's timer anymore because probably the message queue doesn't become empty anymore.
Right now i can only help myself with sleep(xxx). But i want better performance.
Please help :)
Thank you
Edit1: i forgot to mention the point from the topic header. i want to get rid of the timer and call the procedure in frame2 directly.
Edit2: code:
Frame1:
procedure TFrmSerial.TimerSerialTimer(Sender: TObject);
var
resultserial:string;
sl:Tstringlist;
iloop:integer;
begin
if CheckBox1.Checked then
begin
TimerSerialTimer.Enabled:=false;
readString(resultserial); //reads comport data to string
if (resultserial<>'') then
begin
sl:=TStringList.Create;
sl.Sorted:=true;
sl.Duplicates:=dupIgnore;
try
sl.Text:=resultserial;
unit3.DataProcessed:=true;
repeat
if (unit3.DataProcessed=true) then
begin
edit1.Text:=sl[0];
sl.Delete(0);
unit3.DataProcessed:=false;
end
else if (unit3.DataProcessed=false) then
begin
sleep(800);
unit3.DataProcessed:=true; //ugly workaround
end
else
begin
showmessage('undefined state');
end;
until (sl.Count=0);
finally
sl.Free;
end;
end;
TimerSerialTimer.Enabled:=true;
end;
end;
Frame2: code:
procedure TFrmProcessing.Timer1Timer(Sender: TObject);
begin
if self.Visible then
begin
timer1.enabled:=false;
if ProcessString<>ProcessStringBefore then
begin
ProcessStringBefore:=ProcessString;
if length(ProcessString)>2 then DoWork;
end;
unit3.DataProcessed:=true;
timer1.enabled:=true;
end;
end;
TFrame is just a FRAME to handle a block of components together and/or in embedded manner. It has not an own processing thread. For asynchronous processing use TThread objects or (in newer Delphi versions) the Threading library elements.
I don't understand how your frames run in separated threads... But it is not so important. I created an example for each-other controlling threads. It could be more concise but I want to use some interaction not just between the threads but the direction of the user as well. I hope it will be more understandable after some explanatory text.
The Button1Click starts the processing. It starts two processes : the controller and the controlled one. The controlled thread processing until the controller don't trigger a sign to stop working. This sign is sent by the call of the Interrupt method of the TThread instances. This call switch the Interrupted property value of the thread instance to TRUE.
The FALSE state of the CheckBox1.Checked property will stop the controller process and it will notify the other one to stop as well.
The TTestBaseProcess just a common ancestor to do the "processing" and to show the "partial results".
Unit1.pas:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
CheckBox1: TCheckBox;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TTestBaseProcess = class ( TThread )
private
fListBox : TListBox;
fDelay : cardinal;
protected
procedure doSomeComplicatedForAWhile; virtual;
procedure showSomePartialResults; virtual;
public
constructor Create( listBox_ : TListBox; delay_ : cardinal );
end;
TControlledProcess = class ( TTestBaseProcess )
private
fButton : TButton;
protected
procedure Execute; override;
procedure enableButton( enabled_ : boolean ); virtual;
public
constructor Create( listBox_ : TListBox; button_ : TButton );
end;
TControllerProcess = class ( TTestBaseProcess )
private
fCheckBox : TCheckBox;
fControlledThread : TThread;
protected
procedure Execute; override;
public
constructor Create( listBox_ : TListBox; checkBox_ : TCheckBox; controlledThread_ : TThread );
end;
procedure TTestBaseProcess.doSomeComplicatedForAWhile;
begin
sleep( fDelay );
end;
procedure TTestBaseProcess.showSomePartialResults;
begin
Synchronize(
procedure
begin
fListBox.items.add( 'Zzz' );
end
);
end;
constructor TTestBaseProcess.Create( listBox_ : TListBox; delay_ : cardinal );
begin
if ( listBox_ <> NIL ) then
if ( delay_ > 0 ) then
begin
inherited Create( TRUE );
fListBox := listBox_;
fDelay := delay_;
end else
raise Exception.Create( 'Invalid input parameter...' )
else
raise Exception.Create( 'Invalid input parameter...' );
end;
constructor TControlledProcess.Create( listBox_ : TListBox; button_ : TButton );
begin
if ( button_ <> NIL) then
begin
inherited Create( listBox_, 500 );
fButton := button_;
end else
raise Exception.Create( 'Invalid input parameter...' );
end;
procedure TControlledProcess.Execute;
begin
enableButton( FALSE );
while ( not terminated ) do
begin
doSomeComplicatedForAWhile;
showSomePartialResults;
end;
enableButton( TRUE );
end;
procedure TControlledProcess.enableButton( enabled_ : boolean );
begin
Synchronize(
procedure
begin
fButton.Enabled := enabled_;
end
);
end;
constructor TControllerProcess.Create( listBox_ : TListBox; checkBox_ : TCheckBox; controlledThread_ : TThread );
begin
if ( checkBox_ <> NIL ) then
if ( controlledThread_ <> NIL ) then
begin
inherited Create( listBox_, 1000 );
fCheckBox := checkBox_;
fControlledThread := controlledThread_;
end else
raise Exception.Create( 'Invalid input parameter...' )
else
raise Exception.Create( 'Invalid input parameter...' );
end;
procedure TControllerProcess.Execute;
begin
while ( fCheckBox.Checked ) do
begin
doSomeComplicatedForAWhile;
showSomePartialResults;
end;
fControlledThread.terminate;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aT1, aT2 : TThread;
begin
CheckBox1.Checked := TRUE;
ListBox1.Items.Clear;
ListBox2.Items.Clear;
aT1 := TControlledProcess.Create( ListBox1, Button1 );
aT2 := TControllerProcess.Create( ListBox2, CheckBox1, aT1 );
aT1.start;
aT2.start;
end;
end.
Unit1.dfm:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 311
ClientWidth = 423
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ListBox1: TListBox
Left = 8
Top = 39
Width = 201
Height = 266
ItemHeight = 13
TabOrder = 0
end
object Button1: TButton
Left = 8
Top = 8
Width = 201
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object CheckBox1: TCheckBox
Left = 215
Top = 12
Width = 97
Height = 17
Caption = 'CheckBox1'
TabOrder = 2
end
object ListBox2: TListBox
Left = 215
Top = 39
Width = 201
Height = 266
ItemHeight = 13
TabOrder = 3
end
end
I think your problem can be solved with callbacks. Something like this:
type
...
TMyCallback = procedure of Object;
...
of Object means that this procedure should be class method.
If you define variable with this type and than assign some procedure with the same attributes you can call it by calling this variable:
type
TMyCallback = procedure of Object;
TForm2 = class(TForm)
private
...
protected
...
public
callback:TMyCallback;
...
end;
...
procedure Form1.DoSomething;
begin
// do something
end;
procedure Form1.DoSomethingWithEvent;
begin
callback := DoSomething; //assign procedure to variable
if assigned(callback)
callback; //call procedure DoSomething
end;
You should do something like this in your case. It's just example because I didn't see all your code, but I'll try to make it workable:
Frame1:
type
TSerialEvent = function(aResult:String):Boolean of Object;
Frame1 = class(TFrame)
private
...
protected
...
public
...
Callback:TSerialEvent;
end;
...
procedure TFrmSerial.TimerSerialTimer(Sender: TObject);
var
resultserial:string;
sl:Tstringlist;
iloop:integer;
begin
if CheckBox1.Checked then
begin
TimerSerialTimer.Enabled:=false;
readString(resultserial); //reads comport data to string
if (resultserial<>'') then
begin
sl:=TStringList.Create;
sl.Sorted:=true;
sl.Duplicates:=dupIgnore;
try
sl.Text:=resultserial;
repeat
edit1.Text := sl[0];
sl.Delete(0);
if assigned(Callback) then
begin
//Let's call Process method of TFrmProcessing:
if not Callback(edit1.text) then //it's not good idea to use edit1.text as proxy, but we have what we have
raise Exception.Create('Serial string was not processed');
end
else
raise Exception.Create('No Callback assigned');
until (sl.Count=0);
finally
sl.Free;
end;
end;
TimerSerialTimer.Enabled:=true;
end;
end;
Frame2:
You don't need Timer anymore. Everything will be processed in event:
type
TFrmProcessing = class(TFrame)
private
...
protected
...
public
...
function Process(aResult:String):Boolean;
end;
function TFrmProcessing.Process(aResult:String):Boolean;
begin
result := false;
if self.Visible then
begin
if aResult <> ProcessStringBefore then
begin
ProcessStringBefore := aResult;
if length(ProcessString) > 2 then DoWork;
result := true;
end;
end;
end;
And the last thing: you have to assign method Process of TFrmProcessing to Callback of Frame1. I think you should do it at Form1.Create or another method you are using for initialization:
...
procedure Form1.FormCreate(Sender:TObject);
begin
...
Frame1.Callback := FrmProcessing.Process;
...
end;

Listbox (Listing Error)

Hi Im doing a raffle program for my friend.Everything was going good but then when i delete a value,the result was changing... Please help me!
Example:
Listbox;
1-a
2-c
3-b
4-f
5-h
6-j
After delete line 3:
1-a
2-c
4-f
5-h
6-j
6-g
What i want:
1-a
2-c
3-f
4-h
5-j
6-g
Here are the codes:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
ComboBox1: TComboBox;
Edit1: TEdit;
Button1: TButton;
Label2: TLabel;
ComboBox2: TComboBox;
Label3: TLabel;
Button2: TButton;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Button3: TButton;
Button4: TButton;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
Button5: TButton;
Button6: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
b,sayac:integer;
sonkayit,deneme:integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
sayac:=0;
listbox1.MultiSelect:=true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sayac:=sayac+1;
b:=listbox1.Count + 1;
listbox1.Items.Add(IntToStr(b) + ' ' + edit1.Text);
edit1.Text:='';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
a:integer;
kisi:string;
begin
Randomize;
a:=Random(b);
kisi:= listbox1.Items.Strings[a];
edit2.Text:=(kisi);
if combobox1.ItemIndex=0 then
begin
edit2.Visible:=true;
edit3.Visible:=false;
edit4.Visible:=false;
edit5.Visible:=false;
edit6.Visible:=false;
end;
if combobox1.ItemIndex=1 then
begin
edit2.Visible:=true;
edit3.Visible:=true;
edit4.Visible:=false;
edit5.Visible:=false;
edit6.Visible:=false;
end;
if combobox1.ItemIndex=2 then
begin
edit2.Visible:=true;
edit3.Visible:=true;
edit4.Visible:=true;
edit5.Visible:=false;
edit6.Visible:=false;
end;
if combobox1.ItemIndex=3 then
begin
edit2.Visible:=true;
edit3.Visible:=true;
edit4.Visible:=true;
edit5.Visible:=true;
edit6.Visible:=false;
end;
if combobox1.ItemIndex=4 then
begin
edit2.Visible:=true;
edit3.Visible:=true;
edit4.Visible:=true;
edit5.Visible:=true;
edit6.Visible:=true;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
savedialog1.FileName:='çekiliş';
if savedialog1.Execute then
begin
listbox1.Items.SaveToFile(savedialog1.FileName + '.txt');
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if opendialog1.Execute then
begin
listbox1.Items.LoadFromFile(opendialog1.FileName);
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
listbox1.DeleteSelected;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
listbox1.Clear;
end;
end.
listbox1.Items.Add(IntToStr(b) + ' ' + edit1.Text);
instead of directly adding to listbox,store both IntToStr(b) and edit1.Text seperate in two string list,and populate the listbox data from the stringlists.
also performing delete delete from the second stringlist the corresponding index,and repopulate in listbox
or you can just store the edit1.Text in a stringlist,and delete the string from stringlist that you delete from listbox . and populate the data in listbox with the index+string combination.....
I would use a virtual list box here. These are the basic steps:
Store the data in a container other than the GUI control, for instance a string list. This is good practise in any case.
Set the Style to lbVirtual.
Implement on OnData event handler for the list. It needs to return a string composed of the index, and the underlying item in your container.
When you delete an item, delete it from the string list container and call Invalidate on the list box to force a paint cycle. That paint cycle will request new values by calling OnData and your code can supply the updated text.
Whenever the underlying container is modified, you must let the control know how many items it is displaying by setting the Count property of the list box control.
Here is a very simple example:
Pascal unit
unit Unit1;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Forms;
type
TForm1 = class(TForm)
List: TListBox;
Delete: TButton;
procedure FormCreate(Sender: TObject);
procedure ListData(Control: TWinControl; Index: Integer; var Data: string);
procedure DeleteClick(Sender: TObject);
private
FItems: TStringList;
end;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
c: Char;
begin
FItems := TStringList.Create;
for c := 'a' to 'z' do
FItems.Add(c);
List.Count := FItems.Count;
end;
procedure TForm1.ListData(Control: TWinControl; Index: Integer;
var Data: string);
begin
Data := Format('%d %s', [Index+1, FItems[Index]]);
end;
procedure TForm1.DeleteClick(Sender: TObject);
var
Index: Integer;
begin
for Index := FItems.Count-1 downto 0 do
if List.Selected[Index] then
FItems.Delete(Index);
List.Count := FItems.Count;
List.Invalidate;
end;
end.
Associated form file
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 303
ClientWidth = 307
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object List: TListBox
Left = 8
Top = 8
Width = 201
Height = 287
Style = lbVirtual
Anchors = [akLeft, akTop, akRight, akBottom]
MultiSelect = True
TabOrder = 0
OnData = ListData
end
object Delete: TButton
Left = 224
Top = 8
Width = 75
Height = 23
Anchors = [akTop, akRight]
Caption = 'Delete'
TabOrder = 1
OnClick = DeleteClick
end
end

destructor when stopping idhttp.get ( indy, delphi)

My application can download one picture from every url in memo1.
It uses idhttp.get and has a skipbutton. After skip it downloads the next picture.
Q1: Do you have code to put into the destructor and what is the code for " terminate" and "waitfor"?
I found this on another website:
destructor thread.destroy;
begin
try
Terminate;
If HTTP.Connected then HTTP.Disconnect;
finally
WaitFor;
FreeAndNil(HTTP);
end;
inherited;
end;
Q2: How do I call the destructor and make it work?
Q3: Do you have hints (especially security concerns) and additional lines of code?
the code of my application:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP;
type
thread = class
public
Constructor Create; overload;
Destructor Destroy; override;
end;
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Memo1: TMemo;
Memo2: TMemo;
Memo3: TMemo;
startbutton: TButton;
skipbutton: TButton;
procedure startbuttonClick(Sender: TObject);
procedure skipbuttonClick(Sender: TObject);
procedure IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
end;
var
Form1: TForm1;
http: tidhttp;
s: boolean;
implementation
{$R *.dfm}
constructor thread.Create;
begin
HTTP := TIdHTTP.Create(nil);
inherited ;
end;
destructor thread.destroy;
begin
try
If HTTP.Connected then HTTP.Disconnect;
finally
FreeAndNil(HTTP);
end;
inherited;
end;
procedure TForm1.startbuttonClick(Sender: TObject);
var
i: integer;
fs : TFileStream ;
begin
for i:= 0 to memo1.lines.count-1 do begin
s:= false;
fs := TFileStream.Create(inttostr(i)+'abc.jpg', fmCreate);
http:= idhttp1;
try
try
HTTP.Get(memo1.lines[i],fs);
memo2.Lines.add(memo1.Lines[i]);
except
on E: Exception do
begin
memo3.lines.add(' ha ha ha not working '+syserrormessage(getlasterror));
end;
end;
finally
fs.free;
end;
end;
end;
procedure TForm1.skipbuttonClick(Sender: TObject);
begin
s:=true;
end;
procedure TForm1.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
application.ProcessMessages;
if s = true then
http.Disconnect;
end;
end.
Since your are using IdHttp from the GUI (= main thread) and Indy is blocking, you have two options: a) use IdAntifreeze in combination with messages (just drop the component on the form), b) use threads.
Do NOT use Application.Processmessages as it will lead to strange side effects.
now to answer your questions:
Q1: the code you found on the internet implemented solution b) so this is not applicable for your current code
Q2: same as Q1
Q3 : here is a version that correctly implements solution a)
This code is still not 100% perfect as it does not implement logic for disabling/enabling the starttransfer and skiptransfer buttons (I leave that as an exercise for you :) ).
unit Unit16;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
const
WM_TRANSFER = WM_USER + 1;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
IdAntiFreeze1: TIdAntiFreeze;
Memo1: TMemo;
Btn_start: TButton;
Btn_skip: TButton;
Memo2: TMemo;
procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
procedure Btn_startClick(Sender: TObject);
procedure Btn_skipClick(Sender: TObject);
private
{ Private declarations }
Transferring : Boolean;
UrlIndex : Integer;
procedure NextTransfer(var msg : TMessage); message WM_TRANSFER;
procedure StartTransfer;
procedure DoTransfer;
procedure SkipTransfer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.NextTransfer(var msg: TMessage);
begin
DoTransfer;
end;
procedure TForm1.SkipTransfer;
begin
Transferring := false;
end;
procedure TForm1.StartTransfer;
begin
UrlIndex := 0;
DoTransfer;
end;
procedure TForm1.DoTransfer;
var
Url : String;
Stream : TStringStream;
begin
if UrlIndex < Memo1.Lines.Count then
begin
Url := Memo1.Lines[UrlIndex];
Memo2.Lines.Add(Format('getting data from URL: %s', [Url]));
Inc(UrlIndex);
Transferring := True;
try
Stream := TStringStream.Create;
try
IdHttp1.Get(Url, Stream);
Memo2.Lines.Add(Format('Data: "%s"',[Stream.DataString]));
finally
Stream.Free;
end;
except
on E: Exception do
begin
Memo2.Lines.Add(Format('error during transfer: %s', [E.Message]));
end;
end;
Transferring := False;
PostMessage(Handle, WM_TRANSFER, 0, 0);
end;
end;
procedure TForm1.Btn_startClick(Sender: TObject);
begin
Memo2.Lines.Add('starting transfer');
StartTransfer;
end;
procedure TForm1.Btn_skipClick(Sender: TObject);
begin
Memo2.Lines.Add('skipping current transfer');
SkipTransfer;
end;
procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
Memo2.Lines.Add('work event');
if not Transferring and (AWorkMode = wmRead) then
try
Memo2.Lines.Add('disconnecting peer');
IdHttp1.Disconnect;
except
end;
end;
end.
DFM file:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 290
ClientWidth = 707
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 92
Top = 12
Width = 213
Height = 257
Lines.Strings = (
'http://stackoverflow.com'
'http://www.google.com'
'http://www.hardocp.com'
'')
TabOrder = 0
WordWrap = False
end
object Btn_start: TButton
Left = 8
Top = 128
Width = 75
Height = 25
Caption = 'Btn_start'
TabOrder = 1
OnClick = Btn_startClick
end
object Btn_skip: TButton
Left = 8
Top = 159
Width = 75
Height = 25
Caption = 'Btn_skip'
TabOrder = 2
OnClick = Btn_skipClick
end
object Memo2: TMemo
Left = 320
Top = 12
Width = 373
Height = 257
TabOrder = 3
WordWrap = False
end
object IdHTTP1: TIdHTTP
OnWork = IdHTTP1Work
AllowCookies = True
ProxyParams.BasicAuthentication = False
ProxyParams.ProxyPort = 0
Request.ContentLength = -1
Request.ContentRangeEnd = -1
Request.ContentRangeStart = -1
Request.ContentRangeInstanceLength = -1
Request.Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
Request.BasicAuthentication = False
Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
Request.Ranges.Units = 'bytes'
Request.Ranges = <>
HTTPOptions = [hoForceEncodeParams]
Left = 24
Top = 16
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 16
Top = 72
end
end

Jedi USB project read and write Delphi

I am using the Jedi usb hid component to connect to, read and write from a HID device. I have been unable to write to the device. I have been using this code.
type
TReport = Packed record
ReportID: byte;
Data: array [0..64] of byte;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I:integer;
HidData:TReport;
written:DWORD;
begin
hiddata.ReportID:=0;
hiddata.Data[0]:=0;
hiddata.Data[1]:=$80;
for I := 2 to 64 do
hiddata.Data[I]:=$FF;
currentdevice.WriteFile(hiddata,currentdevice.Caps.OutputReportByteLength,written);
end;
I made a test platform which you can use :
unit BasicMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls, Forms, Dialogs,
JvHidControllerClass, JvComponentBase;
type
TReport = packed record
ReportID: byte;
Data: array [0..64] of byte;
end;
TMainForm = class(TForm)
HidCtl: TJvHidDeviceController;
DeviceList: TListBox;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure HidCtlDeviceChange(Sender: TObject);
function HidCtlEnumerate(HidDev: TJvHidDevice;const Idx: Integer): Boolean;
procedure Button1Click(Sender: TObject);
procedure FormCreate( Sender : TObject);
procedure DeviceRemoval(HidDev: TJvHidDevice);
procedure DeviceArrival(HidDev: TJvHidDevice);
public
end;
var
MainForm: TMainForm;
MyDevice: TJvHidDevice;
implementation
{$R *.dfm}
{ ***************************************************************************** }
Const
MyVendorID = $04D8; // Put in your matching VendorID
MyProductID = $003F; // Put in your matching ProductID
procedure TMainForm.FormCreate( Sender : TObject);
begin
HidCtl.OnArrival:= DeviceArrival;
HidCtl.OnRemoval:= DeviceRemoval;
end;
procedure TMainForm.DeviceRemoval(HidDev: TJvHidDevice);
begin
if ((Assigned(MyDevice)) and (NOT MyDevice.IsPluggedIn)) then
begin
HidCtl.CheckIn(MyDevice);
end;
end;
procedure TMainForm.DeviceArrival(HidDev: TJvHidDevice);
begin
if ((HidDev.Attributes.VendorID = MyVendorID) AND
(HidDev.Attributes.ProductID = MyProductID) AND
(HidDev.Caps.OutputReportByteLength = SizeOf(TReport)) ) then
begin
if HidDev.CheckOut then
begin
MyDevice := HidDev;
end;
end;
end;
procedure TMainForm.HidCtlDeviceChange(Sender: TObject);
begin
Label1.Caption := '-';
Label2.Caption := '-';
MyDevice := nil;
DeviceList.Clear;
HidCtl.Enumerate;
end;
function TMainForm.HidCtlEnumerate(HidDev: TJvHidDevice;const Idx: Integer): Boolean;
begin
DeviceList.Items.Add(
Format('%.4x/%.4x', [HidDev.Attributes.VendorID,HidDev.Attributes.ProductID]));
if (HidDev.Attributes.VendorID = MyVendorID) and (HidDev.Attributes.ProductID = MyProductID) then
begin
HidCtl.CheckOut(HidDev);
MyDevice := HidDev;
Label1.Caption := Format('%.4x/%.4x', [MyDevice.Attributes.VendorID , MyDevice.Attributes.ProductID]);
Label2.Caption := 'Length = '+ IntToStr(MyDevice.Caps.OutputReportByteLength) + ' ' + IntToStr(MyDevice.Caps.InputReportByteLength);
end;
Result := True;
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
HidData : TReport;
written : DWORD;
begin
HidData.ReportID:=0;
HidData.Data[0]:=$80;
// Fill with more data
MyDevice.WriteFile(HidData, MyDevice.Caps.OutputReportByteLength, Written);
MyDevice.ReadFile(HidData, MyDevice.Caps.InputReportByteLength, Written);
end;
end.
object MainForm: TMainForm
Left = 0
Top = 0
Caption = 'MainForm'
ClientHeight = 341
ClientWidth = 535
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 48
Top = 8
Width = 31
Height = 13
Caption = 'Label1'
end
object Label2: TLabel
Left = 48
Top = 27
Width = 31
Height = 13
Caption = 'Label2'
end
object Button1: TButton
Left = 48
Top = 46
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ListBox1: TListBox
Left = 48
Top = 96
Width = 465
Height = 97
ItemHeight = 13
TabOrder = 1
end
end
Fill in your VendorID and ProductID and the output data.
Effictivly one line do the trick that the writefile method is accepted or not :
ToWrite := TheDev.Caps.OutputReportByteLength;
TheDev.WriteFile(buffer,towrite,written);
The device accept only write of the correct buffer length. It doesn't work if you write only a part of the buffer length.

Object Pascal: TClientDataset Deletions

I'm creating an in-memory dataset using a TClientDataset to use as a receive buffer. Adding data is great, but once I go to process it I want to be able to remove the row from the dataset. Calling delete works - sort of - the row/index is still accessible but now contains no valid information.
This makes things a bit difficult since when I'm processing this buffer it's not guaranteed that entries will be deleted in fact. I'd rather not start scanning the buffer from the first entry and skipping empty items, so is there a better way to permanently "remove" the item from the dataset? My idea was that it should work something like an actual SQL table where deleting a row doesn't leave empty records.
What's the best way to achieve this, or am I using the wrong component entirely?
By default client datasets mantain a "log" of changes because they are also designed to be able to send client side changes to a remote server, even if they were made in a disconnected session ("briefcase model"). Usually this log is "cleared" when you apply the changes to the remote db, and any other changes is merged with your "local" copy.
Set LogChanges to False if you don't need it and wish that changes are made directly.
There's something wrong with your code. I prepared a test application for this case, because I will face TClientDataSet in Multithreading environment in a few days. My test case application is not presenting this problem (Delphi 2010 Update 5)
I'll publish this code also in my own blog in a couple of days... for now I gave it to you now:
DFM file:
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 337
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 8
Top = 8
Width = 257
Height = 321
Lines.Strings = (
'Memo1')
TabOrder = 0
end
object Button1: TButton
Left = 271
Top = 8
Width = 170
Height = 25
Caption = 'Start'
TabOrder = 1
OnClick = Button1Click
end
object cdsTest: TClientDataSet
Aggregates = <>
Params = <>
Left = 584
Top = 32
object cdsTestNumber: TIntegerField
FieldName = 'Number'
end
end
object tToMemo: TTimer
Enabled = False
Interval = 500
OnTimer = tToMemoTimer
Left = 376
Top = 144
end
end
pas file:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBClient, SyncObjs, ExtCtrls;
type
TWriterThread = class(TThread)
private
FDataSet: TClientDataSet;
//FWriteLock: TMultiReadExclusiveWriteSynchronizer;
FLock: TCriticalSection;
public
constructor Create(ADataSet: TClientDataSet; ALock: TCriticalSection);
procedure Execute; override;
end;
TDeleterThread = class(TThread)
private
FDataSet: TClientDataSet;
//FWriteLock: TMultiReadExclusiveWriteSynchronizer;
FLock: TCriticalSection;
public
constructor Create(ADataSet: TClientDataSet; ALock: TCriticalSection);
procedure Execute; override;
end;
TForm2 = class(TForm)
cdsTest: TClientDataSet;
Memo1: TMemo;
cdsTestNumber: TIntegerField;
Button1: TButton;
tToMemo: TTimer;
procedure Button1Click(Sender: TObject);
procedure tToMemoTimer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FLock: TCriticalSection;
FWriterThread: TWriterThread;
FDeleterThread: TDeleterThread;
procedure cdsToMemo;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
cdsTest.CreateDataSet;
cdsTest.LogChanges := False;
FLock := TCriticalSection.Create;
tToMemo.Enabled := True;
FWriterThread := TWriterThread.Create(cdsTest, FLock);
FDeleterThread := TDeleterThread.Create(cdsTest, FLock);
end;
{ TWriterThread }
constructor TWriterThread.Create(ADataSet: TClientDataSet;
ALock: TCriticalSection);
begin
inherited Create(False);
FDataSet := ADataSet;
FLock := ALock;
end;
procedure TWriterThread.Execute;
var
I: Integer;
begin
inherited;
I := 0;
while not Terminated do
begin
FLock.Enter;
try
Inc(I);
FDataSet.AppendRecord([I]);
finally
FLock.Leave;
end;
Sleep(500); //a new record aproximately each half second
end;
end;
{ TDeleterThread }
constructor TDeleterThread.Create(ADataSet: TClientDataSet;
ALock: TCriticalSection);
begin
inherited Create(False);
FDataSet := ADataSet;
FLock := ALock;
end;
procedure TDeleterThread.Execute;
const
MaxRecords = 100;
var
ProcessedRecords: Integer;
begin
inherited;
while not Terminated do
begin
Sleep(3000); //delete records aproximately every 3 seconds
FLock.Enter;
try
FDataSet.First;
ProcessedRecords := 0;
while (not FDataSet.Eof) and (ProcessedRecords < MaxRecords) do
begin
Inc(ProcessedRecords);
if Odd(FDataSet.Fields[0].AsInteger) then
FDataSet.Delete
else
FDataSet.Next;
end;
finally
FLock.Leave;
end;
end;
end;
procedure TForm2.cdsToMemo;
begin
FLock.Enter;
try
Memo1.Lines.BeginUpdate;
try
Memo1.Lines.Clear;
cdsTest.First;
while not cdsTest.Eof do
begin
Memo1.Lines.Add(cdsTestNumber.AsString);
cdsTest.Next;
end;
finally
Memo1.Lines.EndUpdate;
end;
finally
FLock.Leave;
end;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
tToMemo.Enabled := False;
if cdsTest.Active then
begin
FDeleterThread.Terminate;
FDeleterThread.WaitFor;
FWriterThread.Terminate;
FWriterThread.WaitFor;
end;
end;
procedure TForm2.tToMemoTimer(Sender: TObject);
begin
tToMemo.Enabled := False;
cdsToMemo;
tToMemo.Enabled := True;
end;
end.
I'll no post further explanation, because you seems well versed in multi-threading. If you have any doubt, feel free to comment with questions.
Only one thing... I was planning to use TMultiReadExclusiveWriteSynchronizer to allow better concurrence, but I have no experience in promoting ReadAccess to WriteAccess, so I used a CriticalSection to avoid the time needed to investigate right now.
A couple of remarks regarding your code.
You are using an unusual way to loop through your dataset (using a counter and still using next).
My preferred direction when deleting would be from end to beginning.
You do not post your dataset after delete.
My suggestion would be to try something like this:
MyDataSet.RecNo:= 99
while not MyDataSet.Bof do
begin
fD1 := MyDataset.FieldByName('Field1').AsInteger;
fD2 := MyDataset.FieldByName('Field2').AsInteger;
fD3 := MyDataset.FieldByName('Field3').AsInteger;
if someCondition then
MyDataset.Delete;
MyDataSet.Post;
MyDataset.Previous;
end;

Resources