这是我第一次使用需要通过回调方法将值返回给另一个类的线程。我已经阅读了它,似乎每个人都在使用 AsyncMethodCaller。但是,即使我已经为我的项目添加了必要的引用,VS 2008 仍认为它是未定义的...我在这里可能还做错了什么?
最佳答案
我在 MSDN 文档中没有看到 AsyncMethodCaller,除了作为此处示例代码的一部分(您自己定义 AsyncMethodCaller 委托(delegate)):
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);
}
关于c# - 为什么我不能使用 AsyncMethodCaller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3420175/