2010-08-06 112 views
5

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

回答

9

我没有看到AsyncMethodCaller MSDN文档中,不是这里的一些示例代码部分其他(你定义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); 
} 
+1

哦jeez。感谢捕捉。我没有意识到这篇文章中的代表是在另外一节中定义的!多么尴尬。 :) – Dave 2010-08-06 01:27:57