Reputation: 551
I'm using this code to check my installed programs (appwiz.cpl) and my ipconfig, every 5 seconds.
I'm using a thread since I have a GUI and it needs to stay active.
public void time() {
var time = new System.Threading.Timer(x =>
{
conf();
ncpa("ipconfig /all");
}, null, 5000, Timeout.Infinite);
}
I have my callback at the end of "ncpa" method, as time();
It works, but after a few minutes the threads will exit with code 259, and won't run anymore.
Thanks for the help!
Upvotes: 0
Views: 428
Reputation: 2438
First, you used "Infinite" for the periodic call, which means it won't signal periodically. Moreover, when using time in variable like this it's mays getting collected by GC after you go out of the scope of the "time" variable. Try change and make the "time" variable a field in your class.
If it's instance field, then it would considered to have no references when the object itself won't have references. If you'll make it static field, it'll be considered as a root, and the reference will be held as long as you don't change it manually.
For example:
class Program
{
private static System.Threading.Timer _timer = new Timer(_ => Console.WriteLine("Hi"),
null, 1000, 1000);
static void Main(string[] args)
{
Console.ReadLine();
}
}
Hope this helps.
Upvotes: 1