C#异步操作 [英] C# Asynchronous operation

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

问题描述

实际上我很难理解 BeginInvoke() 和 EndInvoke() 对.

Acctually I have hardtime in understanding BeginInvoke() and EndInvoke() pair.

class AsynchronousDemo
{

    public delegate void DemoDelegate();
    static void Main()
    {

        DemoDelegate d = PrintA;

        IAsyncResult AResult = d.BeginInvoke(Callback,null);
        d.EndInvoke(AResult);
        Console.ReadKey(true);
    }

    static void PrintA()
    {
        Console.WriteLine("....Method in Print A Running ....");
        Thread.Sleep(4000);
        Console.WriteLine("....Method in Print A Completed...");
    }


    static void Callback(IAsyncResult ar)
    {
        Console.WriteLine("I will be finished after method A 
        completes its execution");
    }
}

1) 我们是否使用EndInvoke()"来表示BeginInvoke()..的结束异步操作"?

1) Do we use "EndInvoke()" to indicate the ending "asynchronous operation" of BeginInvoke()..?

2) 这些对的真正用途是什么?

2) What is the real use of those pair?

3) 我能得到一些简单而漂亮的例子来更正确地理解它吗?

3) can i get some simple and nice examples to understand it more properly?

推荐答案

想象一下你有一个很长的任务要做,一次只能做一件事.通常,为了做到这一点,您必须停止做其他所有事情.

Imagine you have a long task to do, and can only do one thing at a time. Normally, in order to do it, you'd have to stop doing everything else.

// pseudocode
Main() {
    DoLaundry()
    GoAboutYourDay()
}

DoLaundry() {
    // boring parts here
}

现在想象一下,您希望能够在洗衣服的同时继续一天的生活.一种解决方案是让其他人来做.所以你把它带到清洁店,告诉他们该怎么做,给他们你的衣服,然后告诉他们完成后给你打电话.作为回报,他们会给你一张票,这样他们就可以在你想要回来时再次找到你的衣服.

Now imagine you want to be able to go about your day while your laundry is being made. One solution would be to get someone else to do it. So you take it to a cleaning shop, tell them what to do, give them your clothes, and tell them to phone you when they're done. In return, they give you back a ticket so they can find your clothes again when you want them back.

// pseudocode
Main() {
   ticket = DoLaundry.BeginDoing(CallMeWhenDone)
   GoAboutYourDay()
   ticket.WaitUntilDone()
}

CallMeWhenDone(ticket) {
   cleanLaundry = DoLaundry.FinishDoing(ticket)
}

这就是异步操作的工作原理.

This is how asynchronous operation works.

BeginInvoke 你告诉程序你需要做什么(委托),完成后调用什么(回调),以及如何处理(状态).您会返回一个 IAsyncResult,这是您需要将其返回以接收结果的对象.然后你可以做其他事情,或者使用 IAsyncResult 中的 WaitHandle 来阻塞直到操作完成.

BeginInvoke You tell the program what you need to be done (the delegate), what to call when it's done (callback), and what to do it with (state). You get back an IAsyncResult, which is the object that you need to give it back in order to receive your result. You can then do other stuff, or use the WaitHandle in the IAsyncResult to block until the operation's done.

回调:当异步操作完成时,它将调用此方法,为您提供与之前相同的 IAsyncResult.此时,您可以从中检索您的状态对象,或将 IAsyncResult 传递给 EndInvoke.

Callback: When the asynchronous operation finishes, it will call this method, giving you the same IAsyncResult as before. At this point, you can retrieve your state object from it, or pass the IAsyncResult to EndInvoke.

EndInvoke: 此函数采用 IAsyncResult 并找到操作的结果.如果它还没有完成,它会阻塞直到它完成,这就是为什么你通常在回调中调用它.

EndInvoke: This function takes the IAsyncResult and finds the result of the operation. If it hasn't finished yet, it'll block until it does, which is why you usually call it inside the callback.

这是一种经常在整个框架中使用的模式,而不仅仅是在函数委托中使用.数据库连接、套接字等通常都有开始/结束对.

This is a pattern that's often used all over the framework, not just on function delegates. Things like database connections, sockets, etc. all often have Begin/End pairs.

MSDN 在此处提供有关该模式的文档:http:///msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx

MSDN has documentation on the pattern here: http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx

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

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