仅在时间延迟后执行方法 [英] Execute a method after delay on time only

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

问题描述

我使用这个方法每 60 秒调用一次另一个方法:

I'm using this method to call another method every 60 seconds:

Timer updateTimer = new Timer(testt, null, 
                              new TimeSpan(0, 0, 0, 0, 1), new TimeSpan(0, 0, 60));

是否可以在延迟 1 毫秒后只调用一次该方法?

It is possible to call this method only once after delay of 1 millisecond?

推荐答案

假设这是一个 System.Threading.Timer,来自 构造函数的最后一个参数:

Assuming this is a System.Threading.Timer, from the documentation for the constructor's final parameter:

期间
回调引用的方法调用之间的时间间隔.指定负一 (-1) 毫秒以禁用定期信令.

period
The time interval between invocations of the methods referenced by callback. Specify negative one (-1) milliseconds to disable periodic signaling.

所以:

Timer updateTimer = new Timer(testt, null,
                              TimeSpan.FromMilliseconds(1),   // Delay by 1ms
                              TimeSpan.FromMilliseconds(-1)); // Never repeat

1 毫秒的延迟真的有用吗?为什么不立即执行呢?如果您真的只是想在线程池线程上执行它,那么有更好的方法来实现这一点.

Is a delay of 1ms really useful though? Why not just execute it immediately? If you're really just trying to execute it on a thread-pool thread, there are better ways of achieving that.

这篇关于仅在时间延迟后执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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