我有一種方法需要很長時間才能完成,並且需要定期檢查它是否完成。這是我想出了(簡化代碼,德爾福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;
在有可能調用isDone實際上做的是什麼做的一部分,完成和假時返回true,上下文同時還有部分工作要做。或者,它可以檢查是否有其他線程完成其工作。我不希望這個對調用者可見。
我想我不是第一個遇到這種類型的問題,這個解決方案可能已經有一個名稱(設計模式?),但我找不到它。 那叫什麼?
這是用於Windows應用程序嗎? – 2009-07-23 09:31:58
是的,但爲什麼這很重要? – dummzeuch 2009-07-23 14:01:15