Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

thread exceptions

1 view
Skip to first unread message

akalb

unread,
Dec 3, 2009, 6:12:43 AM12/3/09
to
How can I using the event OnException on a Thread ?
Here is is my code in a thread and in a form I create the thread and
next I assign it this code but the code in the procedure
myOnException does not work.

{ Public declarations }
procedure myOnException(Sender:TObject);

procedure TMainForm.Button1Click(Sender: TObject);
begin
//creo il thread
ThreadDataCheck := TthrDataCheck.CreateIt;
ThreadDataCheck.OnException:=myOnException;
end;

procedure TMainForm.myOnException(Sender: TObject);
begin
Panel5.Caption:=ThreadDataCheck.LastException;
end;


---------------------------------------------------------------

type
TthrDataCheck = class(TThread)
private
{ Private declarations }
FOnException: TNotifyEvent;

FException: Exception;
procedure DoHandleException;
protected
{ protected declarations }
procedure Execute; override;
procedure HandleException; virtual;
public
{ public declarations }
LastException:string;
published
{ published declarations }
constructor CreateIt;
property OnException:TNotifyEvent read FOnException write
FOnException;
end;

procedure TthrDataCheck.DoHandleException;
begin
// Cancel the mouse capture
if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0,
0);

// gestisco la visualizzazione dell' exception
if FException is Exception then
Application.ShowException(FException)
else
SysUtils.ShowException(FException, nil);
LastException:= FException.Message;

if Assigned(Self.FOnException)then
Self.FOnException(Self);
end;
end;


procedure TthrDataCheck.HandleException;
begin
FException := Exception(ExceptObject);
try
// non visualizzo il messaggio "EAbort"
if not (FException is EAbort) then
Synchronize(DoHandleException);
finally
FException := nil;
end;
end;

constructor TthrDataCheck.CreateIt;
begin
inherited Create (True); // Create thread suspended

FreeOnTerminate := True;
Priority := tpNormal;

FException := nil;
try
Application.MainForm.Caption:=FloatToStr(StrToFloat
(Application.MainForm.Caption) / 2);
except on E: Exception do
begin
LastException:='prova 1 - ';
HandleException;
end;
end;

Suspended := False;
end;

0 new messages