关闭表单

时间:2016-04-08 21:32:14

标签: delphi exception-handling delphi-2010

当我关闭我的应用程序表单并且我不知道如何处理这个时,我有EAccessViolation,我有两个单元,这里是主要单元相关代码:

unit MainUnit;
uses
  .., myComponent1;

implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
  MyComponent1.doSomeWork(p1, p2, ..., pn);
end;
procedure TForm1.OnMyComponen1tEvent(sender: TObject; p: Integer);
begin
  memo1.Lines.Add(message);
end;
end.

本单位使用另一个单位是一个组件类,我在其中向memo1发送信号以显示消息,当然使用组件事件,它是这样的:

unit myComponent;
type
TMyComponentEvent = procedure(sender: TObject; p: integer) of object;
type
TMyComponent = class(TComponent)
// Properties and events declaration
procedure TPThread.Execute;
begin
  try
    // Create and run some worker threads
    // Wait for them to finish the job
    // This is the last thing to do:
    if Assigned(FOnMyComponentEvent) then
    begin
      FOnMyComponentEvent(Self, p);
    end;
 finally
  //free ressources
end;
end;

procedure TMyComponent.DoSomeWork;
begin
  TPThread.Create(p1, p2 ...);
end;
end.

当我在程序完成其作业之前关闭表单(线程仍在工作)时,我得到了该异常,但有时候,没有异常引发。好吧,当引发异常时,它表示行:memo1.Lines.Add(message);。  我不知道如何解决它,那么如何防止异常发生呢?

1 个答案:

答案 0 :(得分:1)

听起来你在销毁表格时没有将MyEvent事件设置为nil,例如

procedure TForm1.FormCreate(sender: TObject);
begin
  OtherUnit.MyEvent := MyEvent;
end;

procedure TForm1.FormDestroy(sender: TObject);
begin
  OtherUnit.MyEvent := nil;
end;