Call a method that returns a value every 5 seconds

c#, c#-2.0

Solution

You could have your caller give the class with the timer a callback delegate to pass back the value.

public class YourClass
{
    public static void Run(string address, Action<string> callback)
    {                     
        Timer t = new Timer();
        t.Elapsed += delegate {                         
            var response = callURL(address);

            callback(response);
        };          
        t.Interval = 3000;
        t.Start();
    }
}

public class OtherClass
{
    public void ProcessResponse(string response)
    {
         // do whatever you want here to handle the response...
         // you can write it out, store in a queue, put in a member, etc.
    }

    public void StartItUp()
    {
         YourClass.Run("http://wwww.somewhere.net", ProcessResponse);
    }
}

UPDATE: If you want the caller (`OtherClass`) to be able to cancel the timer, you could simply change from an `Action<string>` to a `Func<string, bool>` and have the caller (`OtherClass`) return a bool on whether to stop the timer or not...

public class YourClass
{
    public static void Run(string address, Func<string, bool> callback)
    {                     
        Timer t = new Timer();
        t.Elapsed += delegate {                         
            var response = callURL(address);

            // if callback returns false, cancel timer
            if(!callback(response))
            {
                t.Stop();
            }
        };          
        t.Interval = 3000;
        t.Start();
    }
}

public class OtherClass
{
    public bool ProcessResponse(string response)
    {
         // do whatever you want here to handle the response...
         // you can write it out, store in a queue, put in a member, etc.
         // check result to see if it's a certain value...
         // if it should keep going, return true, otherwise return false
    }

    public void StartItUp()
    {
         YourClass.Run("http://wwww.somewhere.net", ProcessResponse);
    }
}

Problem

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?

Original source

Related problems