I have got a method that takes a long time to complete and want to check regularly whether it is done. This is what I have come up with (simplified code, Delphi 2007):
type
IWaitForDone = interface
function IsDone: boolean;
end;
function TSomeClass.doSomethingThatTakesLong: IWaitForDone;
begin
Result := TClassThatDoesIt.Create;
end;
var
Waiter: IWaitForDone;
begin
Waiter := SomeClass.doSomethingThatTakesLong;
while not Waiter.isDone do
doSomethingElse;
Waiter := nil;
end;
In the context it is possible that calling isDone actually does a part of what is to be done and returns true, when finished and false while there are still parts to be done. Alternatively it could just check whether another thread is done with its work. I don't want this to be visible to the caller.
I guess that I am not the first one to come across this type of problem and this solution probably already has got a name (a design pattern?), but I could not find it. So what is it called?