简体   繁体   English

代表检查新数据

[英]Delegates to check new data

I need to check every 5 seconds if there is new data, and if yes, to fire up a delegate. 我需要每5秒检查一次是否有新数据,如果是,则启动一个代理。

How to do this in simple and intuitive way? 如何以简单直观的方式做到这一点?

您可以使用一个简单的Timer来做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM