我怎么可以引发在.NET(每小时或特定的时间间隔)事件每隔一小时? [英] How can I raise an event every hour (or specific time interval each hour) in .NET?

查看:152
本文介绍了我怎么可以引发在.NET(每小时或特定的时间间隔)事件每隔一小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个小的网络爬虫会在系统托盘中运行,并每隔一小时匍匐在小时一个网站。

这是获得.NET每隔一小时或某些其它的时间间隔为引发事件以执行一些任务的最好方法。比如我要运行一个事件基础上的时间每20分钟一班。本次活动将在提高:

  00:20
00:40
01:00
01:20
01:40
 

等。我能想到的做到这一点,最好的方法是创建一个线程循环,可随时检查,如果时间是整除给定的间隔和提出了一个回调事件,如果到达的时间。我觉得好像有已经得到了更好的方法。

我会使用一个定时器但我preFER的东西,得出的小时或类似的规定运行的时间表。

如果没有建立在Windows任务计划我的应用程序这可能吗?

更新:
我将我的算法计算的时间间隔定时器。这个方法接受一个参数,这是什么时候,定时器应触发打勾。例如,如果参数是20,那么计时器会滴答在上述时间表的时间间隔。

  INT CalculateTimerInterval(INT分钟)
{
    如果(分钟< = 0)
        分钟= 60;
    现在的DateTime = DateTime.Now;

    DateTime的未来= now.AddMinutes((分 - (now.Minute%分)))AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1)。

    时间跨度间隔=未来 - 现在;

    返回(INT)interval.TotalMilliseconds;
}
 

这code的使用方法如下:

 静态System.Windows.Forms.Timer吨;
const int的CHECK_INTERVAL = 20;


静态无效的主要()
{
    T =新System.Windows.Forms.Timer();
    t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
    t.Tick + =新的EventHandler(t_Tick);
    t.Start();
}

静态无效t_Tick(对象发件人,EventArgs的)
{
    t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
}
 

解决方案

<一个href="http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx">System.Timers.Timer.如果你想在一天中的特定时间运行,你需要弄清楚它,直到下一次多长时间,并设置为你的时间。

这仅仅是基本的想法。根据如何precise你需要,你可以做得更多。

  INT分钟= DateTime.Now.Minute;
INT调整= 10  - (分10​​%);
timer.Interval =调整* 60 * 1000;
 

I'm working on a little web crawler that will run in the system tray and crawl a web site every hour on the hour.

What is the best way to get .NET to raise an event every hour or some other interval to perform some task. For example I want to run an event every 20 minutes based on the time. The event would be raised at:

00:20
00:40
01:00
01:20
01:40

and so on. The best way I can think of to do this is by creating a loop on a thread, that constantly checks if the time is divisible by a given interval and raises a callback event if the time is reached. I feel like there has got to be a better way.

I'd use a Timer but I'd prefer something that follows a "schedule" that runs on the hour or something along those lines.

Without setting up my application in the windows task scheduler is this possible?

UPDATE:
I'm adding my algorithm for calculating the time interval for a timer. This method takes a "minute" parameter, which is what time the timer should trigger a tick. For example, if the "minute" parameter is 20, then the timer will tick at the intervals in the timetable above.

int CalculateTimerInterval(int minute)
{
    if (minute <= 0)
        minute = 60;
    DateTime now = DateTime.Now;

    DateTime future = now.AddMinutes((minute - (now.Minute % minute))).AddSeconds(now.Second * -1).AddMilliseconds(now.Millisecond * -1);

    TimeSpan interval = future - now;

    return (int)interval.TotalMilliseconds;
}

This code is used as follows:

static System.Windows.Forms.Timer t;
const int CHECK_INTERVAL = 20;


static void Main()
{
    t = new System.Windows.Forms.Timer();
    t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
    t.Tick += new EventHandler(t_Tick);
    t.Start();
}

static void t_Tick(object sender, EventArgs e)
{
    t.Interval = CalculateTimerInterval(CHECK_INTERVAL);
}

解决方案

System.Timers.Timer. If you want to run at specific times of the day, you will need to figure out how long it is until the next time and set that as your interval.

This is just the basic idea. Depending on how precise you need to be you can do more.

int minutes = DateTime.Now.Minute;
int adjust = 10 - (minutes % 10);
timer.Interval = adjust * 60 * 1000;  

这篇关于我怎么可以引发在.NET(每小时或特定的时间间隔)事件每隔一小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆