Delphi 在Ui上显示进度

Delphi 在Ui上显示进度,delphi,Delphi,我有一个简单的应用程序。一个表单和一个数据模块。 我在DataModule中有一些流程,希望在UI中显示进度(使用进度条) 最佳方法是什么?最佳方法是在datamodule上定义一个事件,然后在表单中为该事件实现一个处理程序,并将其分配给datamodule Event。 然后,在流程中,调用事件,从而调用事件处理程序 大概是这样的: type TMyProgressEvent = procedure (Position, TotalSteps: Integer; Msg: string)

我有一个简单的应用程序。一个表单和一个数据模块。 我在DataModule中有一些流程,希望在UI中显示进度(使用进度条)


最佳方法是什么?

最佳方法是在datamodule上定义一个事件,然后在表单中为该事件实现一个处理程序,并将其分配给datamodule Event。 然后,在流程中,调用事件,从而调用事件处理程序

大概是这样的:

type
  TMyProgressEvent = procedure (Position, TotalSteps: Integer; Msg: string) of object

TMyDM = class
private
  FOnProgress: TMyProgressEvent;
....
....
public
  procedure UpdateCustomerOrders;
  property OnProgress: TMyProgressEvent read FOnProgress write FOnProgress;
end

TMyForm = class
....
....
  // you can change the position or progress bar here
  // or if you want to log 
  procedure MyFormProress(Position, TotalSteps: Integer; Msg: string);
end
 procedure TMyDM.UpdateCustomerOrders()
 begin
   for I = 1 to 10 do 
   begin
     ... 
     ... you are processing something 
     ...
     //call event like this
     FOnProgress(I, 12, 'looping');
   end;
   .... another process here
   FOnProgress(11, 12, 'another process');

   .... one more process here
   FOnProgress(12, 12, 'process finished');
 end;
您的TMyDM.UpdateCustomerOrders可能如下所示:

type
  TMyProgressEvent = procedure (Position, TotalSteps: Integer; Msg: string) of object

TMyDM = class
private
  FOnProgress: TMyProgressEvent;
....
....
public
  procedure UpdateCustomerOrders;
  property OnProgress: TMyProgressEvent read FOnProgress write FOnProgress;
end

TMyForm = class
....
....
  // you can change the position or progress bar here
  // or if you want to log 
  procedure MyFormProress(Position, TotalSteps: Integer; Msg: string);
end
 procedure TMyDM.UpdateCustomerOrders()
 begin
   for I = 1 to 10 do 
   begin
     ... 
     ... you are processing something 
     ...
     //call event like this
     FOnProgress(I, 12, 'looping');
   end;
   .... another process here
   FOnProgress(11, 12, 'another process');

   .... one more process here
   FOnProgress(12, 12, 'process finished');
 end;

这似乎并没有回答问题。它假定数据模块上的某些内容生成事件,但如何做到这一点正是OP所要求的。@MartynA,在这种方法中,DM(数据模块)调用事件。活动是通过表格中的方法分配的。是的,我当然理解。但是什么决定了datamodule何时调用事件呢?是的,这要好得多,+1。请记住,这些q和as不仅仅是为了发布q的特定人员的利益。SO的主要价值在于,未来不同编程水平和语言技能的读者也能从中受益。我编辑了你的答案,以遵循更多标准惯例。事实上,您的属性遵循了一些我以前从未见过的约定,我不确定它是否可以编译。这似乎是一个属性和一个过程的混合。这取决于,数据模块中到底发生了什么?@Jerrydoge,我有几个更新数据的循环。客观地定义“最佳”,根据什么标准?同时-1。在其他条件相同的情况下,imo最好的方法不是在gui(VCL)线程中执行datamodule进程,而是在ymmv中执行。