[英]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 can user the Timer for this and use its tick event to be fired up 您可以为此使用计时器,并使用其滴答事件触发
go through this link 通过此链接
http://social.msdn.microsoft.com/Forums/windows/en-US/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/how-to-use-timer-control-in-c or http://social.msdn.microsoft.com/Forums/windows/zh-CN/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/how-to-use-timer-control-in-c或
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx
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.