简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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