在回调方法中获取输入参数 [英] Get input argument inside callback method

查看:100
本文介绍了在回调方法中获取输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码示例中,如何在回调方法 MethodDone中获取输入参数内容?

In the code sample below , how to get input argument content inside callback method "MethodDone" ?

我不想再次将输入参数作为BeginInvoke的第三个参数,因为我想在回调方法中调用EndInvoke。

I don't want to pass the input parameter again as the third argument of BeginInvoke , 'cause I want to call EndInvoke in callback method .

static class Program
{
    static void Main()
    {
        Action<string> del = new Action<string>(SomeMethod);
        del.BeginInvoke("input parameter", MethodDone, del);
    }

    static void MethodDone(IAsyncResult ar)
    {
        //how to get input parameter here ?

        Action<string> del = (Action<string>)ar.AsyncState;
        del.EndInvoke(ar);
    }

    static void SomeMethod(string input)
    {
        //do something 
    }
}


推荐答案

您可以这样写并使用任何东西:

You can write like this and use anything:

static void Main()
    {
        string myInput = "Test";
        Action<string> del = new Action<string>(SomeMethod);
        del.BeginInvoke(
            "input parameter",
            (IAsyncResult ar) =>
                {
                    Console.WriteLine("More Input parameters..." + myInput);
                    del.EndInvoke(ar);
                },
            del);
    }

这篇关于在回调方法中获取输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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