如何异步方法在C#中的工作? [英] How asynchronous methods work in C#?

查看:137
本文介绍了如何异步方法在C#中的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用异步方法在我的一些项目,我喜欢它,因为它可以让我的应用程序是一个很大更具扩展性。不过,我想知道如何异步方法真的管用的背景是什么?如何.NET(或Windows?)知道,一个呼叫完成?根据异步调用我做了一些,我可以看到,新的线程会被创建(并不总是虽然...)。为什么呢?

I am using asynchronous methods in some of my project and I like it since it allows my application to be a lot more scalable. However, I am wondering how asynchronous methods really work in background? How .NET (or Windows?) knows that a call is completed? Depending on the number of asynchronous calls I made, I can see that new threads are created (not always though…). Why?

另外,我想监视多久的请求才能完成。为了测试这个概念,我写了下面code的异步调用Web服务,并立即启动后,一个秒表。

In addition, I would like to monitor how long a request take to complete. To test the concept, I wrote the following code which calls asynchronously a Web service and immediately after starts a stopwatch.

for (int i = 0; i < 10000; i++)
{
    myWebService.BeginMyMethod(new MyRequest(), result, new AsyncCallback(callback), i);
    stopWatches[i].Start();
}
// Call back stop the stopwatch after calling EndMyMethod

这不起作用,因为所有的请求(10000)有相同的开始时间和持续时间将上升线性(称之为0 = 1时长,通话1 = 2时长等)。我怎么能监测与异步方法调用真正的持续时间(从目前的要求是真正执行,直到结束)?

This doesn’t work since all the requests (10000) have the same begin time and the duration will go up linearly (call 0 = duration 1, call 1 = duration 2, etc.). How could I monitor the real duration of a call with asynchronous method (from the moment the request is really executed until the end)?

更新:是否异步方法阻止流动?据我所知,它使用.NET 线程池但如何将的IAsyncResult 知道,一个呼叫完成,它的时间打电话在回调的方法?

UPDATE: Does an asynchronous method block the flow? I understand that it uses the .NET ThreadPool but how an IAsyncResult know that a call is completed and it's time to call the CallBack method?

推荐答案

在code是铁路和线程是火车。随着列车的推移铁路它执行code。

The code is the railroad and the thread is the train. As train goes on railroad it executes the code.

BeginMyMethod 由主线程中执行。如果你看看里面的 BeginMyMethod 它只是增加了的MyMethod 的代表对线程池的队列中。实际的MyMethod 是由火车池的列车之一执行。时调用的MyMethod 完成是由执行同一个线程的的MyMethod 执行完成例程,而不是由你运行的code,其余主线程。当一个线程池中的线程忙于执行的MyMethod ,主线程可以乘坐铁路系统的一些其他部分(执行一些其他的code),或者干脆睡,等到一定的信号灯亮了起来。

BeginMyMethod is executed by the main thread. If you look inside the BeginMyMethod it simply adds a delegate of MyMethod to the ThreadPool's queue. The actual MyMethod is executed by one of the trains of the train pool. The completion routine that is called when MyMethod is done is executed by the same thread that executed the MyMethod, not by your main thread that runs the rest of the code. While a thread pool thread is busy executing MyMethod, the main thread can either ride some other portion of the railroad system (execute some other code), or simply sleep, waiting until certain semaphore is lit up.

因此​​,有没有这样的事情的IAsyncResult 知道何时调用完成例程,相反,完成例程是一个简单的调用线程池中的线程这之后委托执行完毕的MyMethod

Therefore there's no such thing as IAsyncResult "knowing" when to call the completion routine, instead, completion routine is simply a delegate called by the thread pool's thread right after it's done executing MyMethod.

我希望你不介意几分稚气的火车的比喻,我知道解释多线程的人时,它帮助我不止一次。

I hope you don't mind the somewhat childish train analogy, I know it helped me more than once when explaining multithreading to people.

这篇关于如何异步方法在C#中的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆