3

我想调用一个每隔几秒返回一个值的方法。我尝试过使用Timerwith 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

4

2 回答 2

9

您可以让您的调用者给带有计时器的类一个回调委托以传回该值。

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);
    }
}

更新:如果您希望调用者 ( OtherClass) 能够取消计时器,您可以简单地从 an 更改Action<string>为 aFunc<string, bool>并让调用者 ( OtherClass) 返回一个关于是否停止计时器的布尔值......

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);
    }
}
于 2012-05-16T20:18:13.663 回答
0

创建一个Thread并让线程调用你想要的方法,并给线程一个睡眠时间 5 秒:

Thread loopThread = new Thread(new ThreadStart(this.MainLoop));
loopThread.Start();

private void MainLoop()
{
     while(true)
     {
         // Do stuff
         Thread.Sleep(5000);
     }
}
于 2012-05-16T20:16:14.170 回答