我正在运行Windows服务并使用循环和Thread.Sleep来重复任务,使用计时器方法会更好吗?
如果是,代码示例会很棒
我目前正在使用此代码重复
int curMinute;
int lastMinute = DateTime.Now.AddMinutes(-1).Minute;
while (condition)
{
curMinute = DateTime.Now.Minute;
if (lastMinute < curMinute) {
// do your once-per-minute code here
lastMinute = curMinute;
}
Thread.Sleep(50000); // sleeps for 50 seconds
if (error condition that would break you out of this) {
break; // leaves looping structure
}
}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我必须定期发送心跳到"兄弟"应用程序.
使用System.Timers.Timer/Threading.Timer或使用带有while循环和Thread.Sleep的线程可以更好地完成此操作吗?
心跳间隔为1秒.
while(!exit)
{
//do work
Thread.Sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)
要么
myTimer.Start( () => {
//do work
}, 1000); //pseudo code (not actual syntax)...
Run Code Online (Sandbox Code Playgroud) 我需要一些关于在PHP中启动和停止计时器的信息.我需要测量从启动我的.exe程序(我在我的php脚本中使用exec()函数)开始经过的时间,直到它完成执行并显示它花费的时间(以秒为单位).有没有办法如何做到这一点.
谢谢
我想说
var minValue = 0;
if ( typeof callback == 'function' ) {
setTimeout( callback, minValue );
}
Run Code Online (Sandbox Code Playgroud)
当我用JavaScript实现回调函数时这段代码.
但我发现现代浏览器和一些旧浏览器
具有不同的最小超时值.
我知道Zero不能是最小值.
setTimeout的最小值是多少?
现代浏览器和一些旧浏览器的兼容性问题?
我正在构建一个插页式页面,使用<div>和JavaScript,非常简单的脚本但很整洁.
一切正常,但我也想在几秒钟之后关闭div(例如10秒).到目前为止我所拥有的:
要加载div,我有一个像这样的onload函数:
onload="document.getElementById('div1').style.display='block';document.getElementById('div2').style.display='block'"
Run Code Online (Sandbox Code Playgroud)
为了隐藏div,我有一个onclick函数,如下所示:
<a href = "javascript:void(0)" onclick = "document.getElementById('div1').style.display='none';document.getElementById('div2').style.display='none'"></a>
Run Code Online (Sandbox Code Playgroud)
如何使用计时器执行onclick功能,我该怎么办?它也必须是JavaScript.
我想在我的应用程序中触发一个事件,该事件在某一时间持续运行,比如说在下午4点.我想过每秒运行一次计时器,当时间等于下午4点时,运行该事件.这样可行.但我想知道是否有一种方法可以在下午4:00获得回调,而不必继续检查.
我有一个php页面,它回显了数据库中的行.我想每隔30秒通过jquery/ajax调用它.但我也希望能够随时调用页面,这样如果我通过表单添加记录,一旦表单提交,我希望页面通过调用ajax立即更新结果.有人能指出我正确的方向或提供一些基本代码,所以我可以尝试解决这个问题吗?jquery/ajax仍然很新.
我正在尝试创建一个接受Action和Timeout的函数,并在Timeout之后执行Action.该功能是非阻塞的.该函数必须是线程安全的.我也非常,真的想避免使用Thread.Sleep().
到目前为止,我能做的最好的是:
long currentKey = 0;
ConcurrentDictionary<long, Timer> timers = new ConcurrentDictionary<long, Timer>();
protected void Execute(Action action, int timeout_ms)
{
long currentKey = Interlocked.Increment(ref currentKey);
Timer t = new Timer(
(key) =>
{
action();
Timer lTimer;
if(timers.TryRemove((long)key, out lTimer))
{
lTimer.Dispose();
}
}, currentKey, Timeout.Infinite, Timeout.Infinite
);
timers[currentKey] = t;
t.Change(timeout_ms, Timeout.Infinite);
}
Run Code Online (Sandbox Code Playgroud)
问题是从回调本身调用Dispose()不是很好.我不确定"脱落"结束是否安全,即计时器在他们的lambdas执行时被认为是活的,但即使是这种情况,我宁愿妥善处理它.
"一次延迟起火"似乎是一个常见的问题,应该有一个简单的方法来做到这一点,可能是System.Threading中的其他一些库我想念,但是现在我能想到的唯一解决方案是修改上面有一个间隔运行的专用清理任务.有什么建议?
我只想尝试一段代码.伪代码看起来像:
start = get_ticks()
do_long_code()
print "It took " + (get_ticks() - start) + " seconds."
Run Code Online (Sandbox Code Playgroud)
这看起来如何用Python?
更具体地说,我如何得到午夜以来的滴答数(或者Python组织那个时间)?
我正在编写一个每10分钟录制一次音频的Android应用程序.我正在使用Timer来做到这一点.但是schedule和scheduleAtFixedRate有什么区别?使用一个比另一个有任何性能优势吗?
timer ×10
c# ×4
javascript ×3
.net ×1
ajax ×1
android ×1
html ×1
java ×1
jquery ×1
php ×1
python ×1
setinterval ×1
settimeout ×1