我在WPF应用程序中使用System.Timers.Timer.我想了解在计算机休眠和睡眠之后Timer的行为方式.计算机从休眠状态恢复后,我的应用程序出现了一些奇怪的问题.
我应该如何处理计时器,以及它们在计算机处于休眠/睡眠模式时的行为方式?
我有一个午夜计时器,应该每个午夜工作以重置UI上的默认值.
以下是创建计时器的代码:
private void ResetMidnightTimer()
{
// kill the old timer
DisposeMidnightTimer();
_midnightTimer = new Timer();
// scheduling the timer to elapse 1 minute after midnight
_midnightTimer.Interval = (DateTime.Today.AddDays(1).AddMinutes(1) - DateTime.Now).TotalMilliseconds;
_midnightTimer.Elapsed += (_, __) => UpdateRecommendedCollectingTime();
_midnightTimer.Enabled = true;
_midnightTimer.Start();
}
Run Code Online (Sandbox Code Playgroud)
在UI页面的构造函数中,我调用调用ResestMidnightTimer()的方法并在事实上创建计时器.之后,计时器只等了一夜.
当夜间(实际上是12:01 AM)到来时,计时器工作,按预期重置默认值,然后处理现有计时器.最后它为第二天创建了一个新的午夜计时器.但是如果我在那天尝试休眠计算机,午夜计时器将无法工作,也不会重置默认值.
那是因为虽然冬眠它只是将事件处理推迟了相同的时间才被冬眠了吗?
public Button stb;
static int cnt=0;
public ArrayList<RadioButton> Butgrp1 = new ArrayList<RadioButton>();
Timer myt;
TimerTask t;
stb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
myt.mschedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Entering run");
Handler h=new Handler();
h.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Butgrp1.get(cnt).setChecked(true);
cnt=cnt+1;
if(cnt>4)
cnt=0;
if(cnt>0)
// Butgrp1.get(cnt-1).setChecked(false);
System.out.println(cnt);
}
});
}
});
//rg.getChildAt(cnt).setPressed(true);
} …Run Code Online (Sandbox Code Playgroud) 与计时器一起玩.上下文:带有两个标签的winforms.
我想知道如何System.Timers.Timer工作,所以我没有使用Forms计时器.我知道表单和myTimer现在将在不同的线程中运行.是否有一种简单的方法来表示lblValue以下表格中的经过时间?
我在MSDN上看过这里,但有更简单的方法!
这是winforms代码:
using System.Timers;
namespace Ariport_Parking
{
public partial class AirportParking : Form
{
//instance variables of the form
System.Timers.Timer myTimer;
int ElapsedCounter = 0;
int MaxTime = 5000;
int elapsedTime = 0;
static int tickLength = 100;
public AirportParking()
{
InitializeComponent();
keepingTime();
lblValue.Text = "hello";
}
//method for keeping time
public void keepingTime() {
myTimer = new System.Timers.Timer(tickLength);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true;
myTimer.Enabled = true;
myTimer.Start();
}
void …Run Code Online (Sandbox Code Playgroud) 在Qt中,我试图设置一个QTimer每秒调用一个名为"更新"的函数.这是我的.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::update()
{
qDebug() << "update";
}
Run Code Online (Sandbox Code Playgroud)
主要:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
该项目正在构建,但它没有执行更新,因为"更新"行没有显示在任何地方......有人看到我做错了吗?
我在加载页面时不会显示淡入淡出的div:
$('#overlay').fadeIn('fast');
$('#box').fadeIn('slow');
Run Code Online (Sandbox Code Playgroud)
我会在x秒后执行此指令,执行div的fadeOut:
$('#overlay').fadeOut('fast');
$('#box').hide();
Run Code Online (Sandbox Code Playgroud)
我该怎么做?实际上fadeOut是在点击按钮时完成的.
脚本在这里:http ://clouderize.it/cookie-localstorage/a.php单击另一个图像时出现的div将在x秒后消失.非常感谢.
我想安排一个动作来自动更改我的ViewPager页面.我试过了:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
featureViewPager.setCurrentItem(currentPage++, true);
}
}, 100, 500);
Run Code Online (Sandbox Code Playgroud)
但我总是得到:
E/AndroidRuntime(5381): FATAL EXCEPTION: Timer-0
E/AndroidRuntime(5381): java.lang.IllegalStateException: Must be called from main thread of process
Run Code Online (Sandbox Code Playgroud)
我已经在主线程了吗?我怎么解决这个问题?
谢谢你的时间.
编辑:
====================================
感谢您的所有答案.根据这些回复,我遇到了两个解决方案:
解决方案1:
swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentPage == …Run Code Online (Sandbox Code Playgroud) 我正在努力了解DispatchSourceTimer,Timer和asyncAfter之间的主要区别(在我的情况下,该任务需要每X秒运行一次,尽管了解计时器的区别可能对有用)(或者是否有另一个(更有效)中除了列出的计时器外,还可以在Swift中使用调度机制?)。
A Timer需要在其开始的当前队列上有一个活动的运行循环。一个DispatchSourceTimer不需要。A Timer可防止CPU进入空闲状态。这是否也适用于DispatchSourceTimer/ asyncAfter?
在什么情况下,a Timer优于DispatchSourceTimer/ asyncAfter?当然,它们之间的区别是什么?
我想在私人队列中的应用程序中每15秒安排一次工作。这意味着我必须使用,DispatchSourceTimer因为我在不是主线程的队列中(或将runloop添加到队列中并使用Timer)。但是,即使Timer最初使用a,我也看不到任何好处。也许我可以使用另一个操作在私有队列上每X秒执行一次调度工作,该工作比a更为有效DispatchSourceTimer,但是我没有遇到更好的解决方案。
是一个DispatchSourceTimer比一个更有效Timer?还是应该继续使用的自调用方法asyncAfter?
这是创建计时器的代码。
异步之后
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(2)) {
// Code
}
Run Code Online (Sandbox Code Playgroud)
计时器
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { (_) in
// Code
}
Run Code Online (Sandbox Code Playgroud)
DispatchSourceTimer
let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + .seconds(1))
timer.setEventHandler {
// Code
}
timer.activate()
Run Code Online (Sandbox Code Playgroud)
所有计时器的利弊是什么?什么时候应该在另一个之上使用?哪种计时器方式最有效?我想出了以下几点: …
使用C#,如何从System.Timers.Timer对象中获取剩余时间(在经过事件之前)?
换句话说,假设我将计时器间隔设置为6小时,但3小时后,我想知道还剩多少时间.如何让计时器对象显示此时间剩余?
我有一个Windows服务应用程序.并通过在控制台模式下运行来调试它.
在这里http://support.microsoft.com/kb/842793 写道,Timers.Timer有一个错误,而不是在Windows服务中触发.解决方法是使用Threading.Timer本文适用于.NET 1.0和1.1
我使用的是.NET 4,但经过一段时间Threading.Timer也没有开火.那可能是什么原因呢?你能建议什么作为解决方法?
谢谢,
最好的祝福
编辑:我将计时器从Threading.Timer更改为Timers.Timer,它没有任何问题.
<head>
<script>
window.setInterval(function(){timer()},100);
function timer()
{
document.getElementById("timer").innerHTML=
(parseInt(document.getElementById("timer").innerHTML*100)+1)/100;
}
</script>
</head>
<body>
<div id="timer">0.000</div>
</body>Run Code Online (Sandbox Code Playgroud)
如您所见,计时器最多只计算0.29.
为什么?