Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET threading like Node.js/V8?

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.

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:

  • Call long-running functions with callback parameters. (That function would execute on a thread from a pool. Maybe a simple wrapper function to call functions on new threads would be sufficient?)
  • Have those callback function calls ran on the "main" thread for processing
  • Maintain automatic synchronization for all objects accessed by this "main" thread, so locking isn't an issue

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?

like image 302
Brad Avatar asked Sep 25 '12 00:09

Brad


People also ask

Is .NET similar to NodeJs?

ASP.Net is an open source web application framework created by Microsoft. Node. js is an open-source, cross platform JavaScript run-time environment that executes JavaScript code on server-side. ASP.net can be utilized to a modern era site by utilizing web shapes Html 5, JavaScript, CSS.

Is .NET faster than NodeJs?

NET Core has an easier time working with CPU-intensive tasks and rendering static pages since the in-built IIS server kernel caching makes this process very straightforward. Therefore, . NET core vs node.

Is .NET better than NodeJs?

NET Core is definitely a winner in this category. The security and reliability the platform provides make it a great option to create robust software with C# language. Node. js is more reliable for complex enterprise software developed with TypeScript than on its own.

Why V8 is single threaded?

It is single threaded because of the way JavaScript language executes and the fact that node has it's execution happen in event loop. Wherein the above code works on a single thread ( or main thread) .


1 Answers

As others have mentioned, async / await is an excellent choice for .NET. In particular:

  • Task / Task<T> / TaskCompletionSource<T> are analogous to JavaScript's Deferred / Promise / Future.
  • It's pretty easy to create JavaScript-style continuations using .NET-style continuations, but for the most part you won't need them.
  • There is no JavaScript equivalent to async / await. async allows you to write your methods as though they were synchronous, and under the hood it breaks them up into continuations wherever there's an await. So you don't have to use continuation passing style.
  • For operations on a background thread, your best choice is Task.Run. However, the standard pattern for .NET is to have the background operation compute and return a single value, instead of having continuous bidirectional messaging with the main thread.
  • If you do need a "stream" of asynchronous data, you should use TPL Dataflow or Rx. This is where things diverge from JS quite a bit.

I recommend you start with my async / await intro post.

like image 175
Stephen Cleary Avatar answered Oct 27 '22 11:10

Stephen Cleary