我目前正在试验OmniThreadLibrary。随函附上我的代码:
procedure TMainForm.LongWait;
begin
Task := Parallel.Future<string>(
function: string
begin
Sleep(10000);
Result := 'Done';
end,
Parallel.TaskConfig.OnTerminated(
procedure
begin
if Task.IsDone then
MessageDlg('Complete', mtInformation, [mbOK], 0)
else
MessageDlg('Exception', mtError, [mbCancel], 0)
end)
);
end;我会调用LongWait(),它在不阻塞UI的情况下工作正常。我想做的是:
有没有可能用一个非阻塞函数来完成所有这些任务呢?
提前谢谢你,
V。
编辑:添加问题
发布于 2013-09-28 11:57:29
let the task run in the background while waiting for the value
您可以通过几种不同的方式等待结果:
Task.Value,它将阻塞,直到值被计算出来。Task.IsDone,然后在IsDone返回True时调用True。Task.TryValue。OnTerminated)处理程序中的值。if an exception is raised, I want the main thread to catch it
异常将自动转发到代码读取未来结果的位置。由于您没有在任何地方读取结果,所以只需在if assigned(Task.FatalException)处理程序中使用OnTerminated。(顺便说一句,在终止处理程序中,IsDone始终是真的。)
allow the main thread to determine if the task was completed or cancelled
使用Task.IsCancelled。
所有这些都被记录在 chapter of the Parallel Programming with the OmniThreadLibrary的书中。
https://stackoverflow.com/questions/19066165
复制相似问题