标签: timer

如何找到计时器触发的剩余时间?

我需要在x几秒钟后运行一个函数,并具有一定的控制能力(重置计时器,停止计时器,找到剩余的执行时间)。time.Timer非常合适 - 唯一缺少的是它似乎无法找到还剩多少时间。

我有哪些选择?

目前,我在想这样的事情:

package main

import "time"

type SecondsTimer struct {
    T       time.Duration
    C       chan time.Time
    control chan time.Duration
    running bool
}

func (s *SecondsTimer) run() {
    for s.T.Seconds() > 0 {
        time.Sleep(time.Second)
        select {
        case f := <-s.control:
            if f > 0 {
                s.T = f
            } else {
                s.running = false
                break
            }
        default:
            s.T = s.T - 1
        }
    }
    s.C <- time.Now()
}
func (s *SecondsTimer) Reset(t time.Duration) {
    if …
Run Code Online (Sandbox Code Playgroud)

timer go

0
推荐指数
1
解决办法
2129
查看次数

如何在 Java 中启动和停止计时器

我不太擅长Java...

我正在制作一个带有开始、停止和重置按钮的秒表。

当你按开始时,它显然开始了。当您按停止时,它会暂停(使用timer.cancel();)。当你按下复位键时,它会使所有值变为 0,然后停止。当你再次按下开始时,它应该继续计数,它在哪里停止(除非它被重置)。

我正在使用一个java.util.timer; 所以这里的问题是:你如何启动一个暂停(取消)的计时器?例如,如果我使用停止按钮来停止它。在我的代码中,它看起来像这样:timer.cancel();,我怎样才能再次启动它?

我知道你可以使用timer.stop()timer.start()vb

 import java.util.Timer;
 import java.util.TimerTask;

public static void timerStart()
{

        //timer
        if(running == false)
        {

            TimerTask task = new TimerTask()
            { 


                public void run()
                {
                    //what to do at each excecution
                    seconds++;
                    lbl.setText(Short.toString(seconds));
                }
            };

        timer.scheduleAtFixedRate(task,1000,1000);
        }
        running = true;
}
Run Code Online (Sandbox Code Playgroud)

 public static void timerStop()
 {
    timer.cancel();
    runnig = false;
 }
Run Code Online (Sandbox Code Playgroud)

java timer

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

Swift 3 - 即使应用程序在后台运行一个函数

我的 iOS 应用程序遇到了问题。即使我的应用程序不在 iPhone 的前台,我也想每天运行一个函数。我尝试使用NSTimerObject 但如果我的应用程序在后台,它就不起作用。

我怎样才能做到这一点?

注意:我的函数将触发一个通知,该通知根据当天的不同而有所不同。

background timer ios swift swift3

0
推荐指数
1
解决办法
1846
查看次数

Rx Swift 简单计时器不起作用

我在 swift 3 中有这个 RxSwift 代码

let bag:DisposeBag = DisposeBag()
var sig:Observable<Int>!
sig = Observable<Int>.interval(1.0, scheduler: MainScheduler.instance)

sig.subscribe(onNext: { (milsec) in
    print("Mil: \(milsec)")
}).addDisposableTo(bag)
Run Code Online (Sandbox Code Playgroud)

我在点击按钮时运行此代码,但它不会在控制台上打印任何内容。

timer observable rx-swift swift3

0
推荐指数
1
解决办法
2642
查看次数

为什么我的计时器不想在指定的时间间隔内触发?

我的班级中有这两个功能:

func setTimer () {
    alertEndTimer = Timer.init(timeInterval: 10, target: self, selector: #selector(hideAlert), userInfo: nil, repeats: false);
    print ("NOW: \(now)") 
    print ("FIRE DATE: \(alertEndTimer!.fireDate)");
}

@IBAction func hideAlert () {
    doSomething();
}
Run Code Online (Sandbox Code Playgroud)

控制台的结果是这样的:

NOW: 2017-03-01 03:32:36 +0000
FIRE DATE: 2017-03-01 03:32:46 +0000
Run Code Online (Sandbox Code Playgroud)

然后我在 上设置了一个断点doSomething();。但是等了 1 分钟后,根本没有调用断点。我还检查了 timer 是否isValid(),它返回true。那么为什么我的函数没有被调用?

timer swift

0
推荐指数
1
解决办法
1384
查看次数

无法迅速使计时器无效

在我的应用程序中,我应该使用多个计时器,但我不想为每个函数添加单独的计时器,如何创建一个简化创建多个计时器的函数,我在下面尝试了此代码,它可以工作,但我无法使计时器无效。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var first: UILabel!

@IBOutlet weak var second: UILabel!

  var count = 0
  var count2 = 0

  var timer = Timer()
  var timer2 = Timer()

 override func viewDidLoad() {
    super.viewDidLoad()

    timerWithDifferentIntervals(myTimer: timer, interval: 1, target: self, selector: #selector(handle1), userInfo: nil, repeats: true)

    timerWithDifferentIntervals(myTimer: timer2, interval: 1/6, target: self, selector: #selector(handle2), userInfo: nil, repeats: true)
}

func handle1() {
    count += 1
    first.text = "\(count)"
}

func handle2() {
    count2 += 1
    second.text …
Run Code Online (Sandbox Code Playgroud)

timer ios swift

0
推荐指数
1
解决办法
1999
查看次数

如何从c#中的ffmpeg进程获取输出

在我用 WPF 编写的代码中,我在 FFmpeg 中运行了一些过滤器,如果我在终端(PowerShell 或 cmd 提示符)中运行命令,它将逐行为我提供信息。

我正在从 C# 代码调用该过程,它工作正常。我的代码的问题实际上是我无法从我运行的进程中获得任何输出。

我已经为 FFmpeg 过程尝试了 StackOverflow 的一些答案。我在我的代码中看到了 2 个机会。我可以通过 Timer 方法修复它,也可以将事件挂接到 OutputDataReceived。

我尝试了 OutputDataReceived 事件,我的代码从来没有让它工作。我尝试了计时器方法,但仍然没有命中我的代码。请检查下面的代码

        _process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = ffmpeg,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
            },
            EnableRaisingEvents = true
        };

        _process.OutputDataReceived += Proc_OutputDataReceived;

        _process.Exited += (a, b) =>
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                System.Threading.Tasks.Task.Delay(5000);
                System.IO.File.Delete(newName);
            });

            //System.IO.File.Delete()
        };

        _process.Start();
        _timer = new Timer();
        _timer.Interval …
Run Code Online (Sandbox Code Playgroud)

c# ffmpeg timer process

0
推荐指数
1
解决办法
2960
查看次数

64位精度睡眠功能?

目前Sleep需要一个 DWORD(32 位)作为时间。有没有需要 DWORDLONG(64 位)的替代方案?

我正在使用 RNG,其中每增加一个字节,整体等待时间就会增加。使用 32 位整数,总时间为 5 分钟,我想增加它。

c winapi sleep timer

0
推荐指数
1
解决办法
1823
查看次数

使用 Discord.py 制作一个在预定日期发送消息的机器人

我正在尝试制作一个向特定文本频道发送预定消息的机器人。例如在我生日那天说“生日快乐”,或者每天早上说“早上好”。机器人似乎不起作用,因为我的文本频道中没有任何内容。

import discord,random,asyncio,os
from datetime import datetime
from discord.ext import commands

token = '#mytokenhere'
bot=commands.Bot(command_prefix='!')

send_time='01:41' #time is in 24hr format
message_channel_id='0000000000000' #channel ID to send images to

@bot.event
async def on_ready():
    print(bot.user.name)
    print(bot.user.id)

async def time_check():
    await bot.wait_until_ready()
    message_channel=bot.get_channel(message_channel_id)
    while not bot.is_closed:
        now=datetime.strftime(datetime.now(),'%H:%M')
        if now.hour() == 1 and now.minute() == 52:
            message= 'a'
            await message_channel.send(message)
            time=90
        else:
            time=1
        await asyncio.sleep(time)

bot.loop.create_task(time_check())


bot.run('token')
Run Code Online (Sandbox Code Playgroud)

python bots timer discord discord.py

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

F# 我不能从 10 倒计时到 0

我对 F# 很陌生,想尝试做一个简单的倒计时;但是,在下面的代码中,它告诉我“t..0”有问题。

let countdown x =
let mutable t = 10
for t..0 do 
        t=t-1
Run Code Online (Sandbox Code Playgroud)

我希望它在终端中从 10 倒计时到 0。

f# timer

0
推荐指数
1
解决办法
122
查看次数

标签 统计

timer ×10

swift ×3

ios ×2

swift3 ×2

background ×1

bots ×1

c ×1

c# ×1

discord ×1

discord.py ×1

f# ×1

ffmpeg ×1

go ×1

java ×1

observable ×1

process ×1

python ×1

rx-swift ×1

sleep ×1

winapi ×1