简体   繁体   English

Theading.Timer每x秒调用一次方法

[英]Theading.Timer to call a method every x number of seconds

I'm playing with thread timers in my application. 我在应用程序中使用线程计时器。 My code appears to work fine, but the timer doesn't repeat. 我的代码似乎可以正常工作,但计时器不会重复。 It will run once, and then stop/hang. 它将运行一次,然后停止/挂起。 I'm using the following code: 我正在使用以下代码:

new System.Threading.Timer(scrape, null, (this.timeout * 1000), System.Threading.Timeout.Infinite);

this.timeout is set to an int , for example 5 (5 minutes). this.timeout设置为int ,例如5 (5分钟)。

I've checked the docs, and I can't really see any issues. 我检查了文档,但看不到任何问题。 Any help is much appreciated. 任何帮助深表感谢。

You are sending parameters wrong. 您发送的参数错误。

var timer = new System.Threading.Timer(scrape, null, 0, (this.timeout * 1000));

last parameter specifies interval. 最后一个参数指定间隔。

Also 5*1000 milliseconds is 5 seconds not 5 minutes. 5 * 1000毫秒也是5秒而不是5分钟。 if you want 5 minutes then you have to multiply 5 by 60000. 如果要5分钟,则必须将5乘以60000。

this.timeout * 60000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM