I have a simple class with two variables and a Close function which is called from OnTimerTick. On very rare occasions a NullReferenceException is occurring in Close() function, but I fail to understand what those occasions can be. Can somebody explain?
System.Windows.Forms.Timer timer = new Timer();
//timer.Tick is wired up in Constructor to OnTimerTick
private void OnTimerTick(object sender, EventArgs e)
{
timer.Tick -= OnTimerTick;
Close();
}
private void Close()
{
if (varOne != null)
{
varOne.SomeEvent -= onSomeEvent;
varOne.Dispose();
varOne = null;
}
if (varTwo != null)
{
varTwo.AnotherEvent -= onAnotherEvent;
varTwo.Dispose();
varTwo = null;
}
}