views:

197

answers:

4

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?

+3  A: 

Microsoft suggests two patterns for long running methods (invoke asynchronously and rendezvous according to pattern)

Dzmitry Huba
I think the OP might not be looking for alternative solutions but for a name for his particular solution.
Smasher
@smasher: You are right. I just want a name.
dummzeuch
+2  A: 

I've seen this referred to as a 'Future' before.

Here are a couple of links:

This one is by Oren Eini who is a prolific .Net developer. As an aside - it is well worth reading his blog (where this link is from) if you are interested in coding pattterns, best practices etc...

Futures post from Oren Eini (Ayende@Rahien)

And this link is yours but I thought I'd just update the answer with it to be complete.

Futures link from uni-sb.de

Russell Troywest
Interesting reference. I found http://www.ps.uni-sb.de/alice/manual/futures.html with a description of the concept. (Hm, uni-sb? Might that be the university of Saarbruecken? I'll have to check.).
dummzeuch
Thanks for the link dummzeuch. I'll update my answer with it and another link I found, which is where I heard it called a future first I think. Interestingly, the guy I refer to refers to your link.
Russell Troywest
+2  A: 

Not sure if that helps/suits you, but take a look at AsyncCalls unit.

Alexander
+1  A: 

I'm not sure if I understand your problem correctly, but I don't like the idea that part of the actual work is done in the IsDone function (funny sentence). I would expect a method like that to just check some flag and return quickly. What if I decide not to continously call IsDone but to perform some other work and check IsDone after that? Then, no work has been done until the first call - not really what I would expect from an asynchronous call...

I didn't really answer your question, but I doubt that this is a widely used design pattern.

Smasher
In my current case the long running code just sends a few commands to a piece of hardware and waits for the answers. The answers arrive asynchronously in a different thread. So I opted to actually send the commands in the IsDone call and check whether the answers have arrived. OK, it is not actually long running code in that case, it just takes forever to finish without doing much. Using some IPC mechanism is overkill in this case.
dummzeuch