Reputation: 12965
I have thoses objects :
TmyObject1=class
public
Status: integer;
end;
TmyObject2=class
public
StatusA: integer;
StatusB: integer;
end;
I have many objects like those one with many status fields declared. I would like to call a function and let the function update the value of one of the status field. quite easy I declare the function like this :
function MyFunction(var AStatus: Integer);
and I call it like this
MyFunction(myObject1.Status);
or for example
MyFunction(myObject2.StatusB);
that good but now my problem arrive, in MyFunction I create a thread and I want to let the possibility to the thread to update also the value of Status. something like this :
function MyFunction(var AStatus: Integer);
begin
MyTread.??ObjectStatus?? := AStatus;
MyTread.start;
end;
procedure TmyThread.execute;
begin
...
??ObjectStatus?? := NewStatus
...
end;
How can I do ? what type must be the TmyThread.??ObjectStatus??. I was thinking to gave to MyTread a pointer address but I m afraid that if memory relocation between the start and the end of the thread that the pointer address could become wrong (Code must work on ios/android/windows/etc.). any other options to solve my problem ?
Upvotes: 0
Views: 444
Reputation: 47819
Instead of a var parameter you can provide getter and setter to your function:
function MyFunction(GetStatus: TFunc<Integer>; SetStatus: TProc<Integer>);
begin
MyThread.GetStatus := GetStatus;
MyThread.SetStatus := SetStatus;
MyThread.start;
end;
procedure TmyThread.execute;
begin
...
OldStatus := GetStatus;
SetStaus(NewStatus);
...
end;
Calling this function requires some anonymous methods now:
MyFunction(
function: Integer
begin
result := myObject1.Status;
end,
procedure(Arg: Integer)
begin
myObject1.Status := Arg
end);
Of course you have to make sure that myObject1 is available during the thread execution.
Upvotes: 2