当前位置:首页 > Windows程序 > 正文

(译)Asynchronous programming and Threading in C# (.NET 4.5)

2021-05-24 Windows程序

Asynchronous programming and threading is very important feature for concurrent or parallel programming. Asynchronous programming may or may not use threading. Still if we can see them together, I think we may have better understanding of both the features.

在并发、并行编程中,异步编程和线程是非常重要的功能。异步编程可能用到了线程,也可能没有。如果能一起了解他们,我认为我们能更好的了解两者的特点。

Topics covered in this article

Asynchronous programming

Is threading required

Task based Asynchronous Programming

Parallel programming:

Conclusion

文章目录:

异步编程

线程是否是必须的

基于Task的异步编程

并行编程

结论

Asynchronous Programming 异步编程

Asynchronous operation means that the operation runs independent of main or other process flow. In general c# program starts executing from the Main method and ends when the Main method returns. In between all the operations runs sequentially one after another. One operation must wait until its previous operation finishes. Let’s see following code:

异步操作意思是:操作独立于主进程或其他进程。一般的C#程序在主方法执行时开始,在主方法返回时结束。在此期间,所有的操作会一个接一个的顺序执行。一个操作必须等待前一个操作完成。让我们看看下面的代码:

1 static void Main(string[] args) 2 { 3 DoTaskOne(); 4 DoTaskTwo(); 5 }

Method “DoTaskTwo” would not be started until “DoTaskOne” finishes. In other words method “DoTaskOne” blocks the execution as long it takes to finish.

方法DoTaskTwo必须等到DoTaskOne完成才会开始执行。换句话说就是方法DoTaskOne在运行期间阻塞了DoTaskTwo运行。

In asynchronous programming a method is called that runs in the background and the calling thread is not blocked. After calling the method the execution flow immediately backs to calling thread and performs other tasks. Normally it uses Thread or Task (We will discuss Thread and Task in detail later).

在异步编程中,方法调用是在后台运行的,不会阻塞调用的线程;并且在调用方法后立即返回调用线程执行其他任务。通常,我们使用Thread或Task进行异步编程(稍后我们会详细的讨论Thread和Task)。

In our case if we run the “DoTaskOne” asynchronously, after calling the “DoTaskOne” method, execution flow immediately backs to Main method and start “DoTaskTwo”.

在我们的例子中,如果我们异步运行DoTaskOne,在调用DoTaskOne方法后,会立即返回主线程开始执行DoTaskOneTwo。

We can create our own thread using Thread class or use asynchronous patterns provided by .NET to perform asynchronous programming. There are three different asynchronous patterns in .NET:

我们可以通过Thread类或.Net为实现异步编程提供的异步模式创建我们自己的线程。.Net提供了三种不同的编程模式:

1.Asynchronous Programming Model (APM) pattern  异步编程模型(APM)模式

2.Event-based Asynchronous Pattern (EAP)  基于时间的异步编程模式(EAP)

Both the above models are not recommended by Microsoft so we will not discuss about them. If you are interested you can read more from following msdn link:

上面两种模型微软不推荐使用,所以我们不会讨论他们,如果你有兴趣,可以从下面的MSDN链接了解更多:

https://msdn.microsoft.com/en-us/library/ms228963(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms228969(v=vs.110).aspx

3.Task-based Asynchronous Pattern (TAP):基于Task的异步模式 ,此模型推荐使用,因此我们会详细讨论。

Threading is required or not 线程是不是必须的

If we use asynchronous programming pattern that .NET introduced in 4.5, in most of the cases we need not to create manual thread by us. The compiler does the difficult work that the developer used to do.

如果使用.NET 4.5介绍的异步编程模式,大部分情况下我们不需要手动创建线程。编译器做了开发者需要做的困难工作。。

Creating a new tread is costly, it takes time. Unless we need to control a thread, then “Task-based Asynchronous Pattern (TAP)” and “Task Parallel Library (TPL)” is good enough for asynchronous and parallel programming. TAP and TPL uses Task (we will discuss what is Task latter). In general Task uses the thread from ThreadPool(A thread pool is a collection of threads already created and maintained by .NET framework. If we use Task, most of the cases we need not to use thread pool directly. Still if you want to know more about thread pool visit the link:https://msdn.microsoft.com/en-us/library/h4732ks0.aspx)

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70365.html