.NET 4.0 has the TPL which contains the nice Task class to encapsulate aynchronous programming models. I'm working on an app that must be .NET 2.0, but I want to avoid rewriting Task. Any suggestions?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
I know you said you dont want to rewrite Task, but you can actually create something fairly simple using closures, which behaves somewhat like a Task object. This is what I use:
Its a function that calls BeginInvoke() on the delegate you pass in, and returns a function that when called blocks and waits for the result of the function passed in. You'd have to create overloads of this function for different method signatures, of course.
One way to go, you can tweak this to your needs, and add other behaviors too like Continuations, etc. The key is to use closures and anonymous delegates. Should work in .NET 2.0.
EDIT - Here is how you would use it:
You will have to use
System.Threading.Thread
class, you can get theTask
class for .net 3.5 but not for .net 2.Sorry