2013-08-01 62 views
0

我需要每5秒檢查一次是否有新數據,如果是,則啓動一個委託。代表檢查新數據

如何以簡單直觀的方式做到這一點?

回答

0

您可以使用簡單的Timer來做到這一點。

0

你可以有你的來電者給帶定時器的類回調委託回傳值

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