首页 新闻 会员 周边 捐助

C#异步问题lambda

0
[已解决问题] 解决于 2012-12-02 12:48

class Program
    {
        private static int threadId;
        static void Main(string[] args)
        {
            AsyncDemo ad = new AsyncDemo();

            AsyncDelegate dlgt = ad.TestMethod;

            IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, new AsyncCallback(CallbackMethod), dlgt);
            Console.WriteLine("Press Enter to close application.");
            Console.ReadLine();
        }

        static void CallbackMethod(IAsyncResult ar)
        {
            AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState;
            string ret = dlgt.EndInvoke(out threadId, ar);
            Console.WriteLine("The call executed on thread {0},   with return value \"{1}\".", threadId, ret);
        }
    }

    public delegate string AsyncDelegate(int callDuration, out int threadId);
    public class AsyncDemo
    {
        public string TestMethod(int callDuration, out int threadId)
        {
            Console.WriteLine("Test method begins.");
            Thread.Sleep(callDuration);
            threadId = Thread.CurrentThread.ManagedThreadId;
            return "MyCallTime was " + callDuration.ToString(); 
        }
    }

怎样在BeginInvoke的时候直接用一条lambda表达式把回调方法加进去,而不单独定义static void CallbackMethod(IAsyncResult ar)这个方法呢。。高手进

彬彬@科比的主页 彬彬@科比 | 初学一级 | 园豆:43
提问于:2012-12-01 16:02
< >
分享
最佳答案
0
 IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, (o) =>
            {
                AsyncDelegate dlgt1 = (AsyncDelegate)o.AsyncState;
                string ret = dlgt1.EndInvoke(out threadId, o);
                Console.WriteLine("The call executed on thread {0},   with return value \"{1}\".", threadId, ret);
            }, dlgt);
奖励园豆:5
RyanCheng | 菜鸟二级 |园豆:474 | 2012-12-01 19:33

这样写不能实现,,你可以拷过去自己试试。。。这种写法我已经试过的了。就是不行才上来问问的。。就是不知道哪里出了问题!!!!!!!

彬彬@科比 | 园豆:43 (初学一级) | 2012-12-01 19:40
其他回答(3)
0

IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, (r) =>
{
  AsyncDelegate AsyncState = (AsyncDelegate)r.AsyncState;
  string ret = AsyncState.EndInvoke(out threadId, r);
  Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
}, dlgt);

geass.. | 园豆:1821 (小虾三级) | 2012-12-01 16:38

这样写不能实现,,你可以拷过去自己试试。。。这种写法我已经试过的了。就是不行才上来问问的。。就是不知道哪里出了问题!!!!!!!

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-01 19:23

@彬彬@科比: 我肯定是本地试过可以才发上来的,你觉得那里有问题。

支持(0) 反对(0) geass.. | 园豆:1821 (小虾三级) | 2012-12-01 20:20

就是改成这样??你能运行。。能write出来???

    class Program
    {
        private static int threadId;
        static void Main(string[] args)
        {
            AsyncDemo ad = new AsyncDemo();

            AsyncDelegate dlgt = ad.TestMethod;

            IAsyncResult ar = dlgt.BeginInvoke(3000, out threadId, (r) =>
            {
                AsyncDelegate AsyncState = (AsyncDelegate)r.AsyncState;
                string ret = AsyncState.EndInvoke(out threadId, r);
                Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", threadId, ret);
            }, dlgt);

        }

        static void CallbackMethod(IAsyncResult ar)
        {
            AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState;
            string ret = dlgt.EndInvoke(out threadId, ar);
            Console.WriteLine("The call executed on thread {0},   with return value \"{1}\".", threadId, ret);
        }
    }

    public delegate string AsyncDelegate(int callDuration, out int threadId);
    public class AsyncDemo
    {
        public string TestMethod(int callDuration, out int threadId)
        {
            Console.WriteLine("Test method begins.");
            Thread.Sleep(callDuration);
            threadId = Thread.CurrentThread.ManagedThreadId;
            return "MyCallTime was " + callDuration.ToString(); 
        }
    }

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-01 23:19

@geass..: 真试过的??!!

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-01 23:19

@彬彬@科比: 你先自己本地认真测试,阅读MSDN之后再上来发帖。

支持(0) 反对(0) geass.. | 园豆:1821 (小虾三级) | 2012-12-02 08:12

@geass..: 可以了,我有个地方弄错了。谢了

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-02 12:47
0

(arg1,arg2)=>{c#语句};

chenping2008 | 园豆:9836 (大侠五级) | 2012-12-01 16:57
0

上面的 都行

Qlin | 园豆:2403 (老鸟四级) | 2012-12-01 22:48

@geass..: 真试过的??!!

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-01 23:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册