http://blog.csdn.net/henreash/article/details/41349145
The previous article talked about using Ttask to quickly develop applications that run multiple tasks simultaneously, reducing bottlenecks. The next step is to explain the ITask subinterface ifuture.
Ifuture
Ifuture gives Ttask a mechanism that allows us to pass a function that returns a specific type (using generics, the type is determined by <T>). Using the Ifuture instance, run a task and let the main thread continue to the result where we need the task to run. This mechanism allows the code block to be executed in priority, while also ensuring that the results of the task are obtained when needed.
Example
To get a value in the future, first define the worthwhile type, run the task, and get the value. To demonstrate this process, write a meaningless example below (but demonstrate how to use this feature), which is explained in step by stage.
Var
Onevalue:ifuture <Integer>;
Othervalue:integer;
Total:integer;
Begin
Onevalue: = ttask.future<integer> (function: Integer
begin
Result: = computesomething;
Sleep (1000); Delay to show status
end);
MEMO1.LINES.ADD (Trttienumerationtype.
Getname<ttaskstatus> (Onevalue.status));
Othervalue: = computesomething;
MEMO1.LINES.ADD (Trttienumerationtype.
Getname<ttaskstatus> (Onevalue.status));
Total: = Othervalue + onevalue.value;
MEMO1.LINES.ADD (Trttienumerationtype.
Getname<ttaskstatus> (Onevalue.status));
Result
MEMO1.LINES.ADD (total.tostring);
End;
The code output looks like this:
Code key points
First, use ttask.future<t> to define the type of the return value, and then pass an anonymous method that returns the actual value. (This returns an integer from the Computesomething method)
Assigns an instance of the Ifuture type returned by ttask.future to the Onevalue variable.
Onevalue: = ttask.future<integer> (function: Integer
begin
Result: = computesomething;
Sleep (1000); Delay to show status
end);
The sleep operation in an anonymous method doesn't make any sense, but you can get a different value for Onevalue.status after the program is run: waitingtorun,running,completed.
The following code queries the current state of Onevalue. The following code converts the state of an enumeration type to a string type:
Trttienumerationtype.getname<ttaskstatus> (Onevalue.status)
Because the task is ready to execute, the first return value is Waitingtorun. After the first state query, call the Computesomethis task:
Othervalue: = computesomething;
Then look at the status of the Onevalue, which has become operational (because sleep takes longer than the computesomething execution time).
Attention! Does it mean that you need to keep checking the status to determine if you can get the result value already? Of course not.
Total: = Othervalue + onevalue.value;
This line of code tells Onevalue we need his value. Returns the value if the task finishes executing, otherwise it will block waiting for Ifuture to complete. This makes it easy for developers.
This ifuture can run the process you set up and return after completion. is another way to save time and improve efficiency.
Using the Ttask.ifuture (GO) in the XE7 parallel library