What .NET 4.5 (or earlier) higher-level constructs make Threading easier?

前端 未结 4 1546
借酒劲吻你
借酒劲吻你 2021-02-19 23:33

Delegates are a few of the objects that make threading easier in .NET reference. They can be used to asynchronously invoke a method. What other objects exist in framework 4.5 (o

4条回答
  •  盖世英雄少女心
    2021-02-19 23:59

    Well let's see here:

    1. The ThreadPool class - kinda old, but still reliable for a simple producer-consumer pattern.
    2. BackgoundWorker (.NET 2.0+) - another old-school construct, providing useful features for executing tasks in the background in GUI applications.
    3. Timers - useful for executing code at specified intervals using a background thread.
    4. The Task class (.NET 4.0+) - threading abstractions that run on the underlying thread pool and provide many useful features like exception marshaling and scheduling. Useful for the so-called "task parallelism" pattern.
    5. Parallel.For, Parallel.ForEach (.NET 4.0+) - good for executing the same operation over a set of data in parallel. Useful for the so-called "data parallelism" pattern.
    6. Parallel.Invoke (.NET 4.0+) - a further abstraction over Tasks. Simply fires off several pieces of code (methods, lambdas) in parallel.
    7. Concurrent collections (.NET 4.0+) - all you need to pass or share data between threads in an efficient and thread-safe manner.

提交回复
热议问题