Reputation: 585
I'm trying to execute a function periodically, using a System.Threading.Timer
. It calls the function, but it doesn't work, and doesn't report an error. Why?
public class Timerr
{
ArrayList listurl;
ArrayList listcategory;
protected Collection<Rss.Items> list = new Collection<Rss.Items>();
RssManager reader = new RssManager();
System.Threading.Timer Timer;
System.DateTime StopTime;
public void Run()
{
StopTime = System.DateTime.Now.AddMinutes(1);
Timer = new System.Threading.Timer(TimerCallback, null, 0,1000);
}
private void TimerCallback(object state)
{
if (System.DateTime.Now >= StopTime)
{
Timer.Dispose();
return;
}
callrss();
}
}
Upvotes: 4
Views: 3495
Reputation: 38112
This works in LINQPad:
void Main()
{
var t = new Timerr();
t.Run();
Thread.Sleep(60000);
}
public class Timerr
{
System.Threading.Timer Timer;
System.DateTime StopTime;
public void Run()
{
StopTime = System.DateTime.Now.AddMinutes(1);
Timer = new System.Threading.Timer(TimerCallback, null, 0,1000);
}
private void TimerCallback(object state)
{
if (System.DateTime.Now >= StopTime)
{
Timer.Dispose();
return;
}
Console.WriteLine("Hello");
}
}
I recommend you install the free LINQPad with which you can check such things very quickly, without the need to run your entire application
Upvotes: 1
Reputation: 241641
Did you construct an instance of Timerr
? Did you call Run
on that instance? Did you keep that instance around so that the timer isn't GCed (System.Threading.Timer
s aren't automatically rooted, like System.Timers.Timer
s are)? Do you have some busy loop or some other way of keeping your process alive long enough to allow the timer callback to be invoked?
Upvotes: 1