Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:54:37+00:00 2026-06-04T01:54:37+00:00

I want to call a method which returns a value every few seconds. I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:54:39+00:00Added an answer on June 4, 2026 at 1:54 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement: I want to call method every day at any particular
I have a method wrapping some external API call which often returns null .
I want to call a method, pass it the length and have it generate
I Have an application in my account and I want to call a method
I have an application which runs a long task and returns a value. While
i have created a 3tier application.. where i want to call the update method
I have a method in my appDelegate which gets latitude and longitude and returns
I want to call this method: - (void)getUserFriendTargetDialogRequest { currentAPICall = kAPIFriendsForTargetDialogRequests; [self apiGraphFriends];
i want to call a method that placed in tag in master page like
Hi , I want to call a method from a class through ajax. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.