我想调用一个每隔几秒返回一个值的方法。我尝试过使用Timer
with elapsedEventandler
,但在这种情况下,该方法的返回类型为 void。我已经使用TimerTask
该类在 Java 中执行相同的任务。
我希望它在 .NET 2.0 中,因为我正在使用 Visual Studio 2005。
以下是我遇到问题的程序。我尝试使用匿名方法,但response
这种情况下的值在匿名方法之外不存在:
public static string Run(string address)
{
string response = "A";
Timer t = new Timer();
t.Elapsed += delegate
{
response = callURL(address);
console.writeln(response);
// The actual response value is printed here
};
t.Interval = 3000;
t.Start();
Console.WriteLine("response string is " + response);
// response string is A
return response;
}
public static string callURL(string address)
{
className sig = new ClassName();
String responseBody = sig.getURL(address);
return responseBody;
}
如何获取方法response
中的值Run
并将其发送给方法的调用者Run
?