【问题标题】:Why can't I use AsyncMethodCaller?为什么我不能使用 AsyncMethodCaller?
【发布时间】:2011-03-26 02:41:40
【问题描述】:

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

【问题讨论】:

    标签: c# multithreading .net-3.5


    【解决方案1】:

    我在 MSDN 文档中没有看到 AsyncMethodCaller,除了这里的一些示例代码的一部分(您自己定义 AsyncMethodCaller 委托):

    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);
    }
    

    【讨论】:

    • 天哪。谢谢你明白了。我没有意识到委托是在文章前面的不同部分中定义的!多么尴尬。 :)
    猜你喜欢
    • 2020-02-18
    • 2010-10-28
    • 2010-10-28
    • 2022-01-03
    • 2012-07-16
    • 2012-03-13
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多