.NET线程喜欢的Node.js / V8? [英] .NET threading like Node.js/V8?
问题描述
我已经远离.NET桌面编程一段时间后,一边喝着Node.js的koolaid。有Node.js的一些地区,我觉得容易的工作。我特别喜欢的线程模型的简单性,并且我可以有几个多线程应用程序,而只写code的好处,跟踪单个线程的。
I've been away from .NET desktop programming for some time, while drinking the Node.js koolaid. There are some parts of Node.js I find easy to work with. In particular, I like the simplicity of the threading model, and that I can have a few of the benefits of a multithreaded application while only writing code to keep track of a single thread.
现在,我有必要写在.NET中的多线程应用程序,它发生,我认为没有理由我不能使用类似的线程用于建立的Node.js应用的模型。特别是,我想:
Now, I have a need to write a multi-threaded application in .NET, and it occurred to me that there is no reason I cannot use a similar threading model that is used to build Node.js applications. In particular, I want to:
- 呼叫长时间运行的函数回调参数。 (该功能会从池中的线程中执行,也许一个简单的包装函数来调用新线程的功能就足够了?)
- 有那些回调函数调用运行在主线程处理
- 为维护这个主线程访问的所有对象的自动同步,所以锁定不是问题
对于是否已经存在内或.NET应用程序这个线程模型这样的框架?如果不是,是否有.NET的部分已经支持或处理某些功能,我正在找?
Does such a framework for this threading model already exist within, or for .NET applications? If not, are there parts of .NET that already support or handle some of the functionality that I am seeking?
推荐答案
我会推荐的第三方物流。下面是它是如何工作的一个例子
I would recommend the TPL. Here’s an example of how it works
Void Work()
{
Task<string> ts = Get();
ts.ContinueWith(t =>
{
string result = t.Result;
Console.WriteLine(result);
});
}
有一整套的取消可能性,错误使用不同的调度等使用.NET 4.5,你可以选择使用的可能性处理计谋
There are a whole range of possibilities for cancelation, error handling using different schedulers etc. With .Net 4.5 you have the possibility of using await
async void Work()
{
Task<string> ts = Get();
string result = await ts;
Console.WriteLine(result);
}
下面的编译器将查看方式标明异步,并增加了一大堆的线程安全稳健的任务同步code,同时使code可读。
Here the compiler looks at methods marked async and adds a whole pile of thread safe robust task synchronizing code while leaving the code readable.
这篇关于.NET线程喜欢的Node.js / V8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!