F
Frank Rizzo
Consider the code below. Class mytest is instantiated, then set to
null. In its constructor, the class instantiates a timer, which writes
out 'Hello' every 5 seconds. The odd thing is that even after the
mytest object is killed, 'Hello' is still printed every 5 seconds.
Isn't this a bit odd?
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
mytest test = new mytest(); // instantiate an object
test = null; // kill an object
Console.ReadLine();
}
}
public class mytest
{
private Timer timerHello;
public mytest()
{
timerHello = new Timer(
new TimerCallback(timerHello_OnTimer),
new object(), 5000, 5000);
}
private void timerHello_OnTimer(Object stateInfo)
{
Console.WriteLine("Hello");
}
}
}
null. In its constructor, the class instantiates a timer, which writes
out 'Hello' every 5 seconds. The odd thing is that even after the
mytest object is killed, 'Hello' is still printed every 5 seconds.
Isn't this a bit odd?
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
mytest test = new mytest(); // instantiate an object
test = null; // kill an object
Console.ReadLine();
}
}
public class mytest
{
private Timer timerHello;
public mytest()
{
timerHello = new Timer(
new TimerCallback(timerHello_OnTimer),
new object(), 5000, 5000);
}
private void timerHello_OnTimer(Object stateInfo)
{
Console.WriteLine("Hello");
}
}
}