How to manage the return value of a thread?

后端 未结 5 1704
逝去的感伤
逝去的感伤 2020-12-28 20:22

I created a class derived from TThread that executes in background a query.

I want that this class is decoupled from the client.

This kind of th

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 21:05

    Using OmniThreadLibrary:

    uses OtlFutures;
    
    var
      thread: IOmniFuture;
    
    thread := TOmniFuture.Create(
      function: integer;
      begin
        Result := YourFunction;
      end;
    );
    // do something else
    threadRes := thread.Value; //will block if thread is not yet done
    

    Creating the TOmniFuture object will automatically start background thread executing your code. Later you can wait on result by calling .Value or you can use .TryValue or .IsDone to check if the thread has already completed its work.

提交回复
热议问题