.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?
.NET 4.0有TPL,它包含很好的Task类来封装异步编程模型。我正在开发一个必须是.NET 2.0的应用程序,但我想避免重写Task。有什么建议么?
2 个解决方案
#1
13
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:
我知道你说你不想重写Task,但实际上你可以使用闭包创建一些相当简单的东西,它的行为有点像Task对象。这是我用的:
public delegate R AsyncTask<R>();
public static AsyncTask<R> BeginTask<R>(AsyncTask<R> function)
{
R retv = default(R);
bool completed = false;
object sync = new object();
IAsyncResult asyncResult = function.BeginInvoke(
iAsyncResult =>
{
lock (sync)
{
completed = true;
retv = function.EndInvoke(iAsyncResult);
Monitor.Pulse(sync);
}
}, null);
return delegate
{
lock (sync)
{
if (!completed)
{
Monitor.Wait(sync);
}
return retv;
}
};
}
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.
它是一个在传入的委托上调用BeginInvoke()的函数,并返回一个函数,该函数在被调用时会阻塞并等待传入的函数的结果。您必须为不同的方法签名创建此函数的重载,课程。
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.
一种方法,你可以根据需要调整它,并添加其他行为,如Continuations等。关键是使用闭包和匿名委托。应该在.NET 2.0中工作。
EDIT - Here is how you would use it:
编辑 - 以下是如何使用它:
public static string HelloWorld()
{
return "Hello World!";
}
static void Main(string[] args)
{
var task = BeginTask(HelloWorld); // non-blocking call
string result = task(); // block and wait
}
#2
4
You will have to use System.Threading.Thread
class, you can get the Task
class for .net 3.5 but not for .net 2.
您将必须使用System.Threading.Thread类,您可以获取.net 3.5的Task类,但不能获取.net 2的Task类。
Sorry
#1
13
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:
我知道你说你不想重写Task,但实际上你可以使用闭包创建一些相当简单的东西,它的行为有点像Task对象。这是我用的:
public delegate R AsyncTask<R>();
public static AsyncTask<R> BeginTask<R>(AsyncTask<R> function)
{
R retv = default(R);
bool completed = false;
object sync = new object();
IAsyncResult asyncResult = function.BeginInvoke(
iAsyncResult =>
{
lock (sync)
{
completed = true;
retv = function.EndInvoke(iAsyncResult);
Monitor.Pulse(sync);
}
}, null);
return delegate
{
lock (sync)
{
if (!completed)
{
Monitor.Wait(sync);
}
return retv;
}
};
}
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.
它是一个在传入的委托上调用BeginInvoke()的函数,并返回一个函数,该函数在被调用时会阻塞并等待传入的函数的结果。您必须为不同的方法签名创建此函数的重载,课程。
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.
一种方法,你可以根据需要调整它,并添加其他行为,如Continuations等。关键是使用闭包和匿名委托。应该在.NET 2.0中工作。
EDIT - Here is how you would use it:
编辑 - 以下是如何使用它:
public static string HelloWorld()
{
return "Hello World!";
}
static void Main(string[] args)
{
var task = BeginTask(HelloWorld); // non-blocking call
string result = task(); // block and wait
}
#2
4
You will have to use System.Threading.Thread
class, you can get the Task
class for .net 3.5 but not for .net 2.
您将必须使用System.Threading.Thread类,您可以获取.net 3.5的Task类,但不能获取.net 2的Task类。
Sorry