![]() |
Aufrufen einer Unterprozedur mit AsyncCalls
Ich habe eine Prozedur mit Unterprozeduren:
Delphi-Quellcode:
So, wie es da oben steht, funktioniert das natürlich nicht. In Wirklichkeit ist TuWas bei mir eine Klassenmethode von TForm1, und das funktioniert ohne Probleme. Ich hätte es aber aus rein codeökonomischen Gründen gern in einer Unterfunktion. Ich habe auch eine Ahnung, wie man das machen könnte, insbesondere von
procedure TForm1.Haupt;
var i:integer; //------------------------------------------------------- function TuWas(i:integer):integer; begin end; //------------------------------------------------------- procedure RufeAuf; begin TAsyncCalls.Invoke( procedure var i:integer; begin For i := 0 to 10 do If i < 11 then TAsyncCalls.Invoke<integer>(TuWas,i)); end); end; //------------------------------------------------------- begin RufeAuf; end; ![]() |
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Was erwartet Invoke denn als Argument? Daran kann man sehen was geht und was nicht
|
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Delphi-Quellcode:
class function Invoke<T>(Event: TAsyncCallArgGenericMethod<T>; const Arg: T): IAsyncCall; overload; static;
IAsyncCall = interface { Sync() waits until the asynchronous call has finished and returns the result value of the called function if that exists. } function Sync: Integer; { Finished() returns True if the asynchronous call has finished. } function Finished: Boolean; { ReturnValue() returns the result of the asynchronous call. It raises an exception if called before the function has finished. } function ReturnValue: Integer; { ForceDifferentThread() tells AsyncCalls that the assigned function must not be executed in the current thread. } procedure ForceDifferentThread; { added by Zarko Gajic to support canceling tasks waiting in the ThreadPool queue} procedure Cancel; end; TAsyncCallArgGenericMethod<T> = function(Arg: T): Integer of object; |
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Jo, dann gehen da nur Methoden rein - ob der Typ wohl deswegen
Delphi-Quellcode:
(bemerke Method) heißt? :stupid:
TAsyncCallArgGenericMethod
Wenn du das ändern möchtest dann einfach
Delphi-Quellcode:
dann geht da alles rein.
// TAsyncCallArgGenericMethod<T> = function(Arg: T): Integer of object;
TAsyncCallArgGenericMethod<T> = reference to function(Arg: T): Integer; (Den Namen muss man nicht ändern, sollte man aber) |
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Da
Delphi-Quellcode:
als overload deklariert ist, gibt es doch sicher noch andere Möglichkeiten, oder?
Invoke
Z.B. diese
Delphi-Quellcode:
TAsyncCallArgGenericProc<T> = function(Arg: T): Integer; class function Invoke<T>(Proc: TAsyncCallArgGenericProc<T>; const Arg: T): IAsyncCall; overload; static; Das wäre dann die mit der globalen Function, wobei ich dann aber die Object Method vorziehen würde. Leider gibt es aber keine mit
Delphi-Quellcode:
, weswegen der Ansatz aus dem SO Link nicht zieht.
reference to
|
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Dann kann man die ja als overload noch hinzufügen :wink:
|
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Da wollte ich auf Sir Rufo antworten:
"Sind anonyme Methoden nicht auch Methoden? Ich erinnere mich an Lösungen á la
Delphi-Quellcode:
."
M:TMethod
... aber da waren mir die Ereignisse schon voraus. Also OK, ich haue
Delphi-Quellcode:
rein, und dann???
TAsyncCallArgGenericMethod<T> = reference to function(Arg: T): Integer;
|
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Dann kannst du Methoden, Proceduren und anonyme Methoden übergeben.
Delphi-Quellcode:
deckt alle drei Fälle ab.
reference to
Delphi-Quellcode:
Jetzt klar?
TFooMethod = procedure of object; // NUR Methoden
TFooProc = procedure; // NUR Prozeduren TFooAnon= reference to procedure; // Methoden, Prozeduren, Anonyme Methoden |
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Zitat:
|
AW: Aufrufen einer Unterprozedur mit AsyncCalls
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:03 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz