BugHunterUK
BugHunterUK

Reputation: 8958

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).

I've checked the docs, and I can't really see any issues. Any help is much appreciated.

Upvotes: 0

Views: 155

Answers (1)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

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. if you want 5 minutes then you have to multiply 5 by 60000.

this.timeout * 60000

Upvotes: 2

Related Questions