代码之家  ›  专栏  ›  技术社区  ›  Dave

为什么我不能使用AsyncMethodCaller?

  •  5
  • Dave  · 技术社区  · 15 年前

    这是我第一次使用需要通过回调方法将值返回给另一个类的线程。我已经读过了,似乎每个人都在使用AsyncMethodCaller。然而,尽管我已经为我的项目添加了必要的引用,VS2008认为它还没有定义。。。我还能做错什么呢?

    1 回复  |  直到 15 年前
        1
  •  9
  •   Robert Harvey    15 年前

    http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

    部分代码如下(请参见链接了解整个示例):

    using System;
    using System.Threading; 
    
    namespace Examples.AdvancedProgramming.AsynchronousOperations
    {
        public class AsyncDemo 
        {
            // The method to be executed asynchronously.
            public string TestMethod(int callDuration, out int threadId) 
            {
                Console.WriteLine("Test method begins.");
                Thread.Sleep(callDuration);
                threadId = Thread.CurrentThread.ManagedThreadId;
                return String.Format("My call time was {0}.", callDuration.ToString());
            }
        }
        // The delegate must have the same signature as the method
        // it will call asynchronously.
        public delegate string AsyncMethodCaller(int callDuration, out int threadId);
    }