考虑下面的例子:存储任意数据到对象实例
type
TTestClass = class
public
procedure method1; virtual;
end;
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
vmi: TVirtualMethodInterceptor;
ttc: TTestClass;
end;
{ Initially SomeFlag is PostponeExecution }
procedure TForm2.FormCreate(Sender: TObject);
begin
vmi := TVirtualMethodInterceptor.Create(TTestClass);
ttc := TTestClass.Create;
vmi.OnBefore :=
procedure(Instance: TObject; Method: TRttiMethod;
const Args: TArray<TValue>; out DoInvoke: Boolean;
out Result: TValue)
begin
if { SomeFlag = DirectExecution } then
DoInvoke := true
else
begin
{ SomeFlag := DirectExecution }
TThread.CreateAnonymousThread(
procedure
begin
// Invoke() will trigger vmi.OnBefore
// because Instance is the proxified object
// I want to keep "Self" to be the proxified object
Method.Invoke(Instance, Args);
end
).Start;
end
end;
vmi.Proxify(ttc);
ttc.method1;
end;
{ TTestClass }
procedure TTestClass.method1;
begin
// Do something async
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
vmi.Unproxify(ttc);
vmi.Free;
ttc.Free;
end;
欲钩虚拟方法在一个线程即延迟/推迟其执行而执行本身。
为此,我使用TVirtualMethodInterceptor截取给定类的虚拟方法。当一个虚拟方法被调用vmi.OnBefore被触发。这是简化的我的主意的表示:
Call_VirtualMethod(方法1) - > OnBefore_fires_1 - > CreateThread_and_InvokeAgain - > OnBefore_fires_2 - > DoInvoke:= TRUE(即,直接执行该方法)
说明:
最初SomeFlag的值为PostponeExecution。
第一次调用ttc.method1将触发OnBefore事件 (OnBefore_fires_1)。该方法不会执行,因为SomeFlag是 延期执行。因此会创建一个线程,将 SomeFlag设置为DirectExecute,并将再次调用相同的方法,但在线程的上下文中调用 。
然后OnBefore再次开火(因为实例是proxified对象 即该方法是钩状方法)。这次SomeFlag是 DirectExecute并且该方法将被调用。
我在调用方法时使用了proxified对象(Instance var),因为我想让“Self”指向它。这样,如果method1调用同一类的其他虚拟方法,那么稍后也将在线程中自动执行。
为了做到这一点,我需要将标志存储在某处,即表示OnBefore的第二次调用该做什么。 我的问题是如何/在哪里存储“SomeFlag”,以便在OnBefore的两次调用期间可访问? 该解决方案应该是跨平台的。建议/其他解决方案也是受欢迎的。
我想它可以通过VMT修补(link1,link2,link3)完成,但VirtualProtect是Windows的唯一功能,因此违反了跨平台的要求。
任何想法高度赞赏。
这是什么一回事:
想象一下,你可以有这种类的德尔福:
TBusinessLogic = class
public
// Invokes asynchronously
[InvokeType(Async)]
procedure QueryDataBase;
// Invokes asynchronously and automatically return asocciated ITask (via OnBefore event)
[InvokeType(Await)]
function DownloadFile(AUrl: string): ITask;
// This method touches GUI i.e. synchonized
[InvokeType(VclSend)]
procedure UpdateProgressBar(AValue: integer);
// Update GUI via TThread.Queue
[InvokeType(VclPost)]
procedure AddTreeviewItem(AText: string);
end;
...
procedure TBusinessLogic.QueryDataBase;
begin
// QueryDataBase is executed ASYNC (QueryDataBase is tagged as Async)
// Do heavy DB Query here
// Updating GUI is easy, because AddTreeviewItem is tagged as VclPost
for SQLRec in SQLRecords do
AddTreeviewItem(SQLRec.FieldByName["CustomerName"].asString);
end;
这种方法确实简化了线程和同步。没有更多的鸭型TThread.Synchronize(),TThread.Queue()等 你只是专注于业务逻辑和调用适当的方法 - OnBefore事件为你做“脏”的工作。非常接近C#中的等待方法。
这是主要思想!
UPDATE: 我重新编辑的整个问题,使之更加清晰。
为什么downvoting? – 2014-11-21 10:04:56
为什么不将'PostponeExecution'参数添加到你的'InvokeMethod'中并且默认为'True',然后在匿名方法中用'False'调用它呢? – 2014-11-21 10:41:01
@StefanGlienke我无法修改InvokeMethod的签名。我的意思是我为我的问题准备了一个缩小的展示。 InvokeMethod属于一个系统类并具有其他名称。 – 2014-11-21 10:46:10