I'd like to have some work done in a background thread as simple as creating a Future var for it and then asking later for the calculated value.
我想在后台线程中完成一些工作,就像为它创建Future var然后稍后询问计算值一样。
In pseudo-C#-code:
AsyncFuture<int> asyncFuture = new AsyncFuture<int>(FuncToCalculateValue);
//do some other work, or draw UI
if(asyncFuture.NoErrorsHappened){
int realResult = asyncResult.Value;
}
I can implement such type by my own, but my question is: isn't that some kind of a known pattern? Is there maybe a name for it, or maybe even a framework implementation? Probably in .NET 4.0?
我可以自己实现这种类型,但我的问题是:是不是某种已知的模式?是否有它的名称,甚至可能是框架实现?可能在.NET 4.0中?
And if it is a pattern, what are the pitfalls associated with it?
如果是一种模式,与之相关的陷阱是什么?
3 个解决方案
#1
Yacoder, I really like Ayende's two Future implementations. There is some good discussion from Jon Skeet in the comments on the cons.
Yacoder,我真的很喜欢Ayende的两个Future实现。 Jon Skeet在关于缺点的评论中有一些很好的讨论。
#2
Yes, Futures are part of the Task Parallel Library which will be in .NET 4.0.
是的,Futures是任务并行库的一部分,它将在.NET 4.0中。
In .NET 4.0 Beta 1, it looks like this exists as a Task<TResult>
.
在.NET 4.0 Beta 1中,它看起来像Task
#3
also in Java the structure of the code using Future is very similar.
同样在Java中使用Future的代码结构非常相似。
You may be move the "do some other work or draw UI" as a parametric code? This can be viewed as a Template Method.
您可能会将“做其他工作或绘制UI”作为参数代码移动吗?这可以视为模板方法。
#1
Yacoder, I really like Ayende's two Future implementations. There is some good discussion from Jon Skeet in the comments on the cons.
Yacoder,我真的很喜欢Ayende的两个Future实现。 Jon Skeet在关于缺点的评论中有一些很好的讨论。
#2
Yes, Futures are part of the Task Parallel Library which will be in .NET 4.0.
是的,Futures是任务并行库的一部分,它将在.NET 4.0中。
In .NET 4.0 Beta 1, it looks like this exists as a Task<TResult>
.
在.NET 4.0 Beta 1中,它看起来像Task
#3
also in Java the structure of the code using Future is very similar.
同样在Java中使用Future的代码结构非常相似。
You may be move the "do some other work or draw UI" as a parametric code? This can be viewed as a Template Method.
您可能会将“做其他工作或绘制UI”作为参数代码移动吗?这可以视为模板方法。