问题描述
我有一个长期运行的任务,我想要与一个未来异步运行,但我也希望它最终超时。在我看来,我的超时从来没有被调用 - 但也许我没有正确使用超时?
I have a long running task that I want to run asynchronously with a Future, but I also want it to timeout eventually. It seems to me that my timeout is never being called - but perhaps I am not using timeout correctly?
// do actual solution finding asychronously
Future populateFuture = new Future(() {
populateGrid(words, gridWidth, gridHeight);
});
populateFuture.timeout(const Duration(seconds: 3), onTimeout: () {
window.alert("Could not create a word search in a reasonable amount of time.");
});
// after being done, draw it if one was found
populateFuture.then((junk) {
wordSearchGrid.drawOnce();
});
这是在版本1.3.0-dev.4.1也许我只是误会如何使用超时
This is under version 1.3.0-dev.4.1 Perhaps I am just misunderstanding how to use timeout
推荐答案
Dart有。
如果 populateGrid
不允许事件循环切换到超时
部分,将不会执行超时
检查。这意味着您必须通过引入 Future
计算 将 populateGrid
的代码 timeout 函数进行定期检查。
If populateGrid
doesn't allow the event loop to switch to the timeout
part the timeout
checks will not be executed. That means you have to break the code of populateGrid
into several part by introducing Future
computations to allow regular checks by the timeout
function.
这篇关于是Future的超时方法坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!