使用sleep来延迟执行很长一段时间? [英] Using sleep to delay execution for long periods of time?

查看:79
本文介绍了使用sleep来延迟执行很长一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前关于让程序

等待某个日期和时间然后执行某些代码的后续行动的后续行动

到达那里。我的问题是;我可以使用kernel32的sleep函数

来完成这个吗?我关心的是这个函数需要几毫秒

,其中我的程序需要准确到5分钟内,并且延迟

可能长达几天,几个月或者随你。我会以这种方式遇到很多问题吗?我在想如果发生溢出

那么我可以采取Sleep接受的最大值

然后一旦完成功能检查是否时间真的

确实来了,或者是否需要进一步延迟。请告诉我您的想法是什么?b $ b想到这个或者是否有更好的方法来做这件事。


还有一件事,我怎么能够实现这一点,以便执行任务的延迟

完全独立于

程序的其余部分?也就是说,用户仍然可以访问所有表单,

更改设置,并完成所有这些操作。我几乎想要写一个类似于服务的
,但是不知道在哪里开始

就可以了。

This is a follow-up on my previous thread concerning having the program
wait for a certain date and time and then executing some code when it
gets there. My question is; can I use the Sleep function from kernel32
to accomplish this? My concern is that this function takes milliseconds
where my program needs to be accurate to within 5 minuets, and delays
may be as long as a number of days, months or whatever. Would I run into
many problems doing it this way? I was thinking that if an overflow
occurs then I could just take the maximum value that Sleep would accept
and then once that is done having a function check if the time really
did come or if a further delay is needed. Please let me know what you
think of this or if there is a better way to do this.

One more thing, how would I be able to implement this so that the delay
of execution of tasks is completely separate from the rest of the
program? That is, the user should still be able to access all the forms,
change settings, and do all of that. I almost have a desire to write
something like a service, but don''t have a clue of where to even start
on that.

推荐答案

" Max" <毫安***** @ yahoo.com> schrieb
"Max" <ma*****@yahoo.com> schrieb
这是我之前关于让
程序等待某个日期和时间然后在它到达时执行一些
代码的跟进。我的问题是;我可以使用kernel32的Sleep
函数来完成这个吗?我担心的是,这个
函数需要几毫秒才能使我的程序在5分钟内准确到达,并且延迟可能长达几天,几个月或者其他什么。这样做会遇到很多问题吗?我在想如果发生溢出,那么我可以只接受睡眠可以接受的最大值,然后一旦完成了功能检查,确定时间是否确实来了或是否
需要进一步延迟。请让我知道您对此的看法,或者是否有更好的方法来做到这一点。


为什么不使用计时器来处理它的Elapsed事件?

还有一件事,我怎么能够实现这个呢?延迟执行任务与程序的其余部分是完全分开的吗?也就是说,用户仍然可以访问所有表单,更改设置,并完成所有这些操作。我几乎想要写一些类似服务的东西,但是没有任何线索可以从哪里开始。
This is a follow-up on my previous thread concerning having the
program wait for a certain date and time and then executing some
code when it gets there. My question is; can I use the Sleep
function from kernel32 to accomplish this? My concern is that this
function takes milliseconds where my program needs to be accurate to
within 5 minuets, and delays may be as long as a number of days,
months or whatever. Would I run into many problems doing it this
way? I was thinking that if an overflow occurs then I could just
take the maximum value that Sleep would accept and then once that is
done having a function check if the time really did come or if a
further delay is needed. Please let me know what you think of this
or if there is a better way to do this.
Why not use a Timer and handle it''s Elapsed event?
One more thing, how would I be able to implement this so that the
delay of execution of tasks is completely separate from the rest of
the program? That is, the user should still be able to access all
the forms, change settings, and do all of that. I almost have a
desire to write something like a service, but don''t have a clue of
where to even start on that.




如果你使用System.Timers.Timer,Elapsed事件发生在一个单独的

线程中。


-

Armin


如何报价以及原因:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



If you use the System.Timers.Timer, the Elapsed event occurs in a separate
thread.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


" ;最大" <毫安***** @ yahoo.com> schrieb
"Max" <ma*****@yahoo.com> schrieb
这是我之前关于让
程序等待某个日期和时间然后在它到达时执行一些
代码的跟进。我的问题是;我可以使用kernel32的Sleep
函数来完成这个吗?我担心的是,这个
函数需要几毫秒才能使我的程序在5分钟内准确到达,并且延迟可能长达几天,几个月或者其他什么。这样做会遇到很多问题吗?我在想如果发生溢出,那么我可以只接受睡眠可以接受的最大值,然后一旦完成了功能检查,确定时间是否确实来了或是否
需要进一步延迟。请让我知道您对此的看法,或者是否有更好的方法来做到这一点。


为什么不使用计时器来处理它的Elapsed事件?

还有一件事,我怎么能够实现这个呢?延迟执行任务与程序的其余部分是完全分开的吗?也就是说,用户仍然可以访问所有表单,更改设置,并完成所有这些操作。我几乎想要写一些类似服务的东西,但是没有任何线索可以从哪里开始。
This is a follow-up on my previous thread concerning having the
program wait for a certain date and time and then executing some
code when it gets there. My question is; can I use the Sleep
function from kernel32 to accomplish this? My concern is that this
function takes milliseconds where my program needs to be accurate to
within 5 minuets, and delays may be as long as a number of days,
months or whatever. Would I run into many problems doing it this
way? I was thinking that if an overflow occurs then I could just
take the maximum value that Sleep would accept and then once that is
done having a function check if the time really did come or if a
further delay is needed. Please let me know what you think of this
or if there is a better way to do this.
Why not use a Timer and handle it''s Elapsed event?
One more thing, how would I be able to implement this so that the
delay of execution of tasks is completely separate from the rest of
the program? That is, the user should still be able to access all
the forms, change settings, and do all of that. I almost have a
desire to write something like a service, but don''t have a clue of
where to even start on that.




如果你使用System.Timers.Timer,Elapsed事件发生在一个单独的

线程中。


-

Armin


如何报价以及原因:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



If you use the System.Timers.Timer, the Elapsed event occurs in a separate
thread.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


Armin Zingler写道:
Armin Zingler wrote:
" Max" <毫安***** @ yahoo.com> schrieb

"Max" <ma*****@yahoo.com> schrieb

这是我之前关于让
程序等待某个日期和时间然后执行一些的线程的后续行动
代码到达那里。我的问题是;我可以使用kernel32的Sleep
函数来完成这个吗?我担心的是,这个
函数需要几毫秒才能使我的程序在5分钟内准确到达,并且延迟可能长达几天,几个月或者其他什么。这样做会遇到很多问题吗?我在想如果发生溢出,那么我可以只接受睡眠可以接受的最大值,然后一旦完成了功能检查,确定时间是否确实来了或是否
需要进一步延迟。请让我知道您对此的看法
或者是否有更好的方法。
This is a follow-up on my previous thread concerning having the
program wait for a certain date and time and then executing some
code when it gets there. My question is; can I use the Sleep
function from kernel32 to accomplish this? My concern is that this
function takes milliseconds where my program needs to be accurate to
within 5 minuets, and delays may be as long as a number of days,
months or whatever. Would I run into many problems doing it this
way? I was thinking that if an overflow occurs then I could just
take the maximum value that Sleep would accept and then once that is
done having a function check if the time really did come or if a
further delay is needed. Please let me know what you think of this
or if there is a better way to do this.



为什么不使用计时器并处理它'' s Elapsed事件?



Why not use a Timer and handle it''s Elapsed event?

还有一件事,我将如何实现这一点,以便延迟执行任务完全独立于
该计划?也就是说,用户仍然可以访问所有表单,更改设置,并完成所有这些操作。我几乎想要写一些类似服务的东西,但是没有任何线索可以从哪里开始。
One more thing, how would I be able to implement this so that the
delay of execution of tasks is completely separate from the rest of
the program? That is, the user should still be able to access all
the forms, change settings, and do all of that. I almost have a
desire to write something like a service, but don''t have a clue of
where to even start on that.


线程中。



If you use the System.Timers.Timer, the Elapsed event occurs in a separate
thread.




多少CPU功率如果我让计时器运行一个数字

天?这真的是我唯一关注的问题,如果它几乎不需要任何

资源并且足够准确,那么我认为这解决了我的问题。



How much CPU power will it take if I have the timer running for a number
of days? That''s really my only concern, if it doesn''t take almost any
resources and is accurate enough then I think that solves my problem.

这篇关于使用sleep来延迟执行很长一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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