I want to call a method which returns a value every few seconds.
I have tried using a Timer
with elapsedEventandler
, but the return type of the method is void in this case. I have used the TimerTask
class to perform the same task in Java.
I want it to be in .NET 2.0 as I'm using Visual Studio 2005.
Below is the program I'm having trouble with. I tried to use an anonymous method, but the value of response
in this case does not exist outside the anonymous method:
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;
}
How do I get the value of response
in the Run
method and send it to the caller of the Run
method?