时间问题 [英] Timing Question

查看:60
本文介绍了时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



嘿,每个人都有一个问题


i正在编写一个应用程序,这个应用程序需要什么?
每小时检查数据库,看看它们是否是下一个小时内需要处理的任何信息



所以我的应用程序必须连接到数据基地(例如)

5:00:00然后检查所有到期的工作

5:00:00

5:15:00

5:30:00

5:45:00


现在我没有问题连接到数据库并获取即将到来的小时所需的
工作列表...我的问题是时间安排


i希望我的应用程序触发一个名为(CheckForNewBatches)每小时

,它确实,,,但如果我的应用程序启动,例如

4:30:27我希望它等到下一个小时这将是5:00:00(兴奋地

其他明智的sql不会返回任何东西)所以即使秒应该是

零。所以在新的Timer方法中,我必须给它等待下一个小时的数量

毫秒..但我总是得到一个错误的小时..对于

例如,如果我在4:30开始我的申请,方法不会开火直到

5:00:58这不是我想要的..我希望它是正确的

5: 00:00 ....有人可以告诉他们我的代码看错了吗


所以在我的consturctor中我写了类似的东西


autoEvent = new AutoResetEvent(false);

timerTicked = new TimerCallback(CheckForNewBatches);

checkerTimer = new

Timer(timerTicked,autoEvent ,MilliSecondsToDelay,36 00000);

===== EndofConstructor

//应返回等待下一小时的毫秒数

internal int MilliSecondsToDelay {

get {


if(startTime == DateTime.MinValue)

startTime = DateTime.Now;

if(startTime.Minute!= 0)

return(3600000 - (startTime) .Minute * 60000 -

startTime.Second * 1000));

else {

onHour = new DateTime(startTime.Year,

startTime.Month,startTime.Day,startTime.Hour,0,0);

返回0;

}

}

}

private void CheckForNewBatches(对象发件人){

///做任何事情

} < br $> b $ b ---

最好的问候

Amir


***通过Developersdex发送 http://www.developersdex.com ***


Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

推荐答案

Amir,


为什么不按照计划设置

机器上的任务?让您的应用程序执行

只执行您需要执行的任务的逻辑(而不是时间逻辑),然后让计划任务管理器运行任务小时。

这样,你不用担心它。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" Amir Ghezelbash" <是******* @ hotmail.com>在消息中写道

新闻:%2 *************** @ TK2MSFTNGP10.phx.gbl ...
Amir,

Instead of having your application perform this timing, why not just set
it up as a scheduled task on the machine? Have your application perform
only the logic to perform the tasks you need it to do (not the timing
logic), and then have the scheduled task manager run the task ever hour.
This way, you don''t have to worry about it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

嘿,每个人都有一个问题

我正在编写一个应用程序,这个应用程序需要在每小时基础上检查数据库,看看它们是否是任何信息需要在下一个小时内处理
所以我的应用程序必须立即连接到数据库(例如)
5:00:00然后检查所有到期的作业
5:00:00
5:15:00
5:30:00
5:45:00
现在我连接没问题到数据库并获取即将到来的小时所需的工作列表......我的问题是时间安排

我希望我的应用程序在每小时,它确实,,,但如果我的应用程序开始例如
4:30:27我希望它等到下一个小时,这将是5:00:00(令人兴奋的
其他明智的sql不会返回任何东西)所以偶数秒应该为零。所以在新的Timer方法中,我必须给它等待下一个小时的
毫秒数。但是我总是得到一个错误的小时......例如,如果我在4:30开始我的应用程序方法不会开火直到5:00:58这不是我想要的..我希望它是正确的在5:00:00 ....可以有人告诉他们看到的是错的用我的代码

所以在我的consturctor中我写了类似的东西

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
定时器(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);

===== EndofConstructor

//应返回的数量等待下一个小时的毫秒数
internal int MilliSecondsToDelay {
如果(startTime == DateTime.MinValue)
startTime = DateTime.Now;

if(startTime.Minute!= 0)
返回(3600000 - ( startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month,startTime.Day,startTime 。小时,0,0);
返回0;
}
}
}

private void CheckForNewBatches(Object sender){
///做任何事情
}
---最好的问候
Amir

***通过Developersdex发送 http://www.developersdex.com ***

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***



您好,

您的代码没有任何问题。并且你无能为力

解决它:)


你不能确定你的应用程序将在5:00:00完全开始运行,在

那个时刻发送事件并稍后处理,所以这是正常的

需要更长的时间等待。

类似的事情,如果你按照Paldino的建议,简而言之,WinXX不是一个实时的操作系统。


欢呼,


-

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局


Amir Ghezelbash <是******* @ hotmail.com>在消息中写道

新闻:%2 *************** @ TK2MSFTNGP10.phx.gbl ...
Hi,
There is nothing wrong with your code. and there is nothing you can do to
solve it :)

You cannot be sure that your app will start running at EXACTLY 5:00:00 , at
that moment an event is send and processed later on, so it''s normal it will
take a little longer to wait.

A similar thing if you follow Paldino''s suggestions, in short WinXX is not a
real time OS.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Amir Ghezelbash" <am*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...

嘿,每个人都有一个问题

我正在编写一个应用程序,这个应用程序需要在每小时基础上检查数据库,看看它们是否是任何信息需要在下一个小时内处理
所以我的应用程序必须立即连接到数据库(例如)
5:00:00然后检查所有到期的作业
5:00:00
5:15:00
5:30:00
5:45:00
现在我连接没问题到数据库并获取即将到来的小时所需的工作列表......我的问题是时间安排

我希望我的应用程序在每小时,它确实,,,但如果我的应用程序开始例如
4:30:27我希望它等到下一个小时,这将是5:00:00(令人兴奋的
其他明智的sql不会返回任何东西)所以偶数秒应该为零。所以在新的Timer方法中,我必须给它等待下一个小时的
毫秒数。但是我总是得到一个错误的小时......例如,如果我在4:30开始我的应用程序方法不会开火直到5:00:58这不是我想要的..我希望它是正确的在5:00:00 ....可以有人告诉他们看到的是错的用我的代码

所以在我的consturctor中我写了类似的东西

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
定时器(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);

===== EndofConstructor

//应返回的数量等待下一个小时的毫秒数
internal int MilliSecondsToDelay {
如果(startTime == DateTime.MinValue)
startTime = DateTime.Now;

if(startTime.Minute!= 0)
返回(3600000 - ( startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month,startTime.Day,startTime 。小时,0,0);
返回0;
}
}
}

private void CheckForNewBatches(Object sender){
///做任何事情
}
---最好的问候
Amir

***通过Developersdex发送 http://www.developersdex.com ***

Hey every body i had a question

i am in process of writing an application, where this application needs
to check the database on hourly bases to see if they are any information
that are needed to be processed in the next upcoming hour
so my application has to to connect to data base right on (for example)
5:00:00 then check all the jobs that are due on
5:00:00
5:15:00
5:30:00
5:45:00

now i have no problem connecting to database and getting the list of
jobs that are due on that upcoming hour...my question is in timing

i want my application to fire a method called (CheckForNewBatches) on
each hour, which it does,,,but if my application starts on for example
4:30:27 i want it to wait till next hour which would 5:00:00 (excatly
other wise sql wouldnot return any thing) so even seconds should be
zero. so on new Timer method i have to give it the number of
milliseconds to wait to next hour..but i always get a wrong hour..for
example if i start my application at 4:30 the method wouldnot fire till
5:00:58 which is not what i want ..i want it to be right on
5:00:00....can some one tell what they see is wrong with my code

so in my consturctor i wrote something like that

autoEvent = new AutoResetEvent(false);
timerTicked = new TimerCallback(CheckForNewBatches);
checkerTimer = new
Timer(timerTicked,autoEvent,MilliSecondsToDelay,36 00000);
=====EndofConstructor
// Should return the number of milliseconds to wait untill next hour
internal int MilliSecondsToDelay {
get {

if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (startTime.Minute != 0)
return (3600000 - (startTime.Minute * 60000 -
startTime.Second * 1000));
else {
onHour = new DateTime(startTime.Year,
startTime.Month, startTime.Day, startTime.Hour, 0, 0);
return 0;
}
}
}
private void CheckForNewBatches(Object sender){
///Do whatever
}
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***



嗨感谢您的回复人员


但是由于这个exe必须

一直在运行,我可以使用schdeule taks ...它有很多东西,例如管理一个

webapplication数据库,如果我要使用Windows计划任务管理器

i必须每小时创建一个新的应用程序实例我...

真的不想这样做......它会让我的整个逻辑变得沉重......窗口

会耗尽内存:P ......其他任何建议?< br $> b $ b ---

最好的问候

Amir


***通过Developersdex发送 http://www.developersdex.com ***
hi thanks for your replies guys

but i canot use the schdeule taks due to the fact that this exe has to
be running all the time ...it does alot of things like managing a
webapplication Database, if i were to use windows schedule task manager
i have to create a new instance of the application every hour...which i
dont really want to do ..it would desotry my whole logic plus... windows
would run out of memory :P ....any other suggestions?
---
Best Regards
Amir

*** Sent via Developersdex http://www.developersdex.com ***

这篇关于时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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