标签: timer

是否连续暂停/停止和启动/恢复Java TimerTask?

我有一个关于Java TimerTask的简单问题.如何根据特定条件暂停/恢复两个TimerTask任务?例如,我有两个在彼此之间运行的计时器.当在第一计时器的任务内满足某个条件时,第一计时器停止并启动第二计时器,并且当在第二计时器的任务内满足某个条件时发生相同的事情.下面的课程显示了我的意思:

public class TimerTest {
    Timer timer1;
    Timer timer2;
    volatile boolean a = false;

    public TimerTest() {
        timer1 = new Timer();
        timer2 = new Timer();     
    }

    public void runStart() {
        timer1.scheduleAtFixedRate(new Task1(), 0, 1000);
    }

    class Task1 extends TimerTask {
        public void run() {
            System.out.println("Checking a");
            a = SomeClass.getSomeStaticValue();

            if (a) {
                // Pause/stop timer1, start/resume timer2 for 5 seconds
                timer2.schedule(new Task2(), 5000);
            }
        }
    }

    class Task2 extends TimerTask{
        public void run() {
            System.out.println("Checking a");
            a = SomeClass.getSomeStaticValue(); …
Run Code Online (Sandbox Code Playgroud)

java multithreading android asynchronous timer

19
推荐指数
4
解决办法
9万
查看次数

C# - System.Timers.Timer的替代方案,用于在特定时间调用函数

我想在特定时间调用我的C#应用​​程序上的特定函数.起初我想过使用a Timer (System.Time.Timer),但很快就无法使用了.为什么?

简单.Timer类需要一个Interval毫秒,但考虑到我可能希望执行该函数,让我们在一周内说,这意味着:

  • 7天= 168小时;
  • 168小时= 10,080分钟;
  • 10,080分钟= 604,800秒;
  • 604,800秒= 604,800,000毫秒;
  • 所以间隔为604,800,000;

现在让我们记住,Interval接受的数据类型是int,并且我们知道int范围从-2,147,483,648到2,147,483,647.

这使得Timer无用,不是在这种情况下,但是在超过25天的情况下,一旦我们无法设置Interval更大的2,147,483,647毫秒.


所以我需要一个解决方案,我可以指定何时应该调用该函数.像这样的东西:

solution.ExecuteAt = "30-04-2010 15:10:00";
solution.Function = "functionName";
solution.Start();
Run Code Online (Sandbox Code Playgroud)

因此,当系统时间到达"30-04-2010 15:10:00"时,该功能将在应用程序中执行.

怎样才能解决这个问题?


附加信息:这些功能将起什么作用?

  • 根据该信息获取气候信息:
  • 启动/关闭其他应用程序(大多数是基于控制台的);
  • 向这些控制台应用程序发送自定义命令;
  • 断电,重启,睡眠,休眠计算机;
  • 如果可能的话,安排BIOS启动计算机;

编辑:

这似乎是在Interval接受数据类型double,但是如果你设置的值越大,一个intInterval,并调用Start()它抛出一个异常[0, Int32.MaxValue].

编辑2:

JørnSchou -Rode建议使用Ncron来处理调度任务,起初看起来这似乎是一个很好的解决方案,但我想听听一些使用它的人.

c# timer scheduled-tasks

19
推荐指数
3
解决办法
2万
查看次数

立即激活.net System.Timers.Timer

我正在使用Timer在相当长的间隔(2分钟)内定期运行事件.这工作正常.但是我希望在创建计时器时立即触发事件(而不是等待2分钟).

请注意,我不能仅通过调用方法来执行此操作,因为它需要一些时间来运行并会阻止应用程序.我需要正常触发计时器并在单独的线程中运行事件.

目前我能想到的最好的方法是对计时器进行子类化并创建一个TriggerManually类似这样的方法:

  • 关闭自动重置
  • 将间隔设置为1毫秒
  • 启用计时器

这将立即触发已经过去的事件,我可以将所有设置恢复正​​常.

虽然看起来有点迂回.有没有更好的方法呢?

timer

19
推荐指数
3
解决办法
1万
查看次数

执行时间超过span时的计时器行为?

我正在编写Windows服务,每隔几分钟就会处理一些"东西".

这是一些代码:

public Service()
        {
            this.InitializeComponent();
            this.ServiceName = Name;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;

            this.eventLog.Source = Name;

            // initialize timer
            this.timer.Elapsed += this.TimerElapsed;
        }

        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);

            if (this.processor.PrepareToRun())
            {
                this.processor.Run();
            }
        }
Run Code Online (Sandbox Code Playgroud)

我想知道如果this.processor.Run()需要很长时间会发生什么,下一次TimerElapsed活动会被提出?它会跳过吗?完成后会等待并尽快运行吗?我应该考虑这些场景和代码吗?

我正在使用 System.Timers.Timer

编辑:

private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);

            try
            {
                this.timer.Stop();
                if (this.processor.PrepareToRun())
                {
                    this.processor.Run();
                }
            }
            catch (Exception ex)
            {
                LoggingAndNotifications.LogAndNotify(ex);

            }
            finally
            {
                this.timer.Start();
            }
        } …
Run Code Online (Sandbox Code Playgroud)

.net c# windows-services timer

19
推荐指数
1
解决办法
6327
查看次数

你如何使用TimerTask来运行一个线程?

我很难在Android上找到TimerTask函数的文档.我需要使用TimerTask间隔运行一个线程,但不知道如何去做.任何建议或例子将不胜感激.

multithreading android timer timertask

19
推荐指数
3
解决办法
5万
查看次数

PowerShell显示已用时间

我有一个脚本启动一个事件,并等待用户按任意键来停止脚本执行.我正在尝试找到一种方法来显示一个计时器(脚本运行了多长时间),同时在Read-Host上等待用户输入.有没有办法实现这个目标?


这有效

$Time = [System.Diagnostics.Stopwatch]::StartNew()
while ($true) {
    $CurrentTime = $Time.Elapsed
    write-host $([string]::Format("`rTime: {0:d2}:{1:d2}:{2:d2}",
                                  $CurrentTime.hours,
                                  $CurrentTime.minutes,
                                  $CurrentTime.seconds)) -nonewline
    sleep 1
    if ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
        Write-Host "Exiting now"
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

powershell timer

19
推荐指数
4
解决办法
5万
查看次数

如何使用HTML,CSS或JavaScript创建循环倒数计时器?

目前,我正在开展一个问答游戏,对于每个问题,我希望放置一个倒数计时器.我有一些插件,但我希望自己可以创建它.我想要创建的内容看起来像下图中的那个.你能告诉我怎么做吗?

有没有办法将边框分配到最多只有指定百分比的周长,这样我就可以先给出一个边框,然后随着每一个进展,我可以继续减少/增加它以便我得到它以完美的方式.

我希望创建的计时器应该看起来像这样(希望你了解它的蓝色边框每秒会增加):

在此输入图像描述

html javascript css timer

19
推荐指数
1
解决办法
4万
查看次数

如何在 swiftUI 中 x 秒后触发操作

我一直在努力实现两个令我头疼的主要目标。对不起,如果这是一个简单的修复,我对 swift/swiftui 还是有点陌生

  1. 经过一定时间后触发动作。
  2. @State根据经过的时间触发改变值。

我搜索了 Stack Overflow 并找到了建议使用计时器的答案:

struct CurrentDateView : View {
    @State var now = Date()

    let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()

    var body: some View {
        Text("\(now)")
            .onReceive(timer) {
                self.now = Date()
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我将如何合并它以便@State可以使用类似的东西false在 7.5 秒过去后将我的值更改为:

@State randomTF : Bool = true
Run Code Online (Sandbox Code Playgroud)

或 7.5 秒后Text("Please Enter Above")更改为 Text("Sorry Too Late")

xcode timer swift swiftui

19
推荐指数
4
解决办法
1万
查看次数

同步计时器以防止重叠

我正在编写一个Windows服务,每隔一段时间运行一次可变长度的活动(数据库扫描和更新).我需要经常运行此任务,但要处理的代码并不安全地同时运行多次.

我怎样才能最简单地设置一个计时器来每隔30秒运行一次任务,而不会重复执行?(我假设System.Threading.Timer这个工作是正确的计时器,但可能会弄错).

c# multithreading timer overlap

18
推荐指数
3
解决办法
2万
查看次数

UNIX编程.struct timeval如何打印它(C编程)

我正在尝试打印timeval类型的值.实际上我可以打印它,但我得到以下警告:

此行有多个标记

  • 格式'%ld'需要类型'long int',但参数2的类型为'struct timeval'

程序编译并打印出值,但我想知道我做错了什么.谢谢.

    printf("%ld.%6ld\n",usage.ru_stime);
    printf("%ld.%6ld\n",usage.ru_utime);
Run Code Online (Sandbox Code Playgroud)

用法类型

typedef struct{
    struct timeval ru_utime; /* user time used */
    struct timeval ru_stime; /* system time used */
    long   ru_maxrss;        /* maximum resident set size */
    long   ru_ixrss;         /* integral shared memory size */
    long   ru_idrss;         /* integral unshared data size */
    long   ru_isrss;         /* integral unshared stack size */
    long   ru_minflt;        /* page reclaims */
    long   ru_majflt;        /* page faults */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block …
Run Code Online (Sandbox Code Playgroud)

unix timer rusage getrusage

18
推荐指数
2
解决办法
7万
查看次数