这是我第一次使用需要通过回调方法将值返回到另一个类的线程。我已经阅读了它,似乎每个人都在使用AsyncMethodCaller。但是,即使我在项目中添加了必要的引用,VS 2008仍认为它是未定义的……在这里我可能还会做错什么呢?
最佳答案
除了作为此处的某些示例代码的一部分(您自己定义AsyncMethodCaller委托(delegate))之外,我没有在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);
}