J
joe
Hi,
I am sorry to ask similar questions again, but I could not
find answers anywhere. My question is this:
Why, even after calling Dispose on a control, which has
subscribed for timer events, the control still receive
events? The only way to fix the problem is to unsubscribe
from the Tick event in the control's Disposed event.
class MyFormublic Form
{
MyUserControl myControl;
public System.Windows.Timer Timer t=new Timer();
Foo()
{
if (myControl!=null) myControl.Dispose();
myControl=new MyUserControl();
}
}
class MyUserControl:...
{
MyUserControl()
{
MyForm.t.Tick+=Timer_Tick(...)
}
Timer_Tick(...)
{// gets fired even for non-existing
//controls
}
}
// somewhere in the code call Foo twice
Foo();
..........
Foo();
Now the timer event will be fired twice-for the control
that is currently assigned to myControl and for the old
control. Why does this happen for the old one? After
assigning a new instance to myControl the old one should
be GC (or become eligible for it), particularly after
calling Dispose on it. Even if it is not CG, why should it
get events? After all, if no references to it exist, this
is a mark that no one is interested in the control, let
alone the events it had subscriptions for.
Thanks,
joe
I am sorry to ask similar questions again, but I could not
find answers anywhere. My question is this:
Why, even after calling Dispose on a control, which has
subscribed for timer events, the control still receive
events? The only way to fix the problem is to unsubscribe
from the Tick event in the control's Disposed event.
class MyFormublic Form
{
MyUserControl myControl;
public System.Windows.Timer Timer t=new Timer();
Foo()
{
if (myControl!=null) myControl.Dispose();
myControl=new MyUserControl();
}
}
class MyUserControl:...
{
MyUserControl()
{
MyForm.t.Tick+=Timer_Tick(...)
}
Timer_Tick(...)
{// gets fired even for non-existing
//controls
}
}
// somewhere in the code call Foo twice
Foo();
..........
Foo();
Now the timer event will be fired twice-for the control
that is currently assigned to myControl and for the old
control. Why does this happen for the old one? After
assigning a new instance to myControl the old one should
be GC (or become eligible for it), particularly after
calling Dispose on it. Even if it is not CG, why should it
get events? After all, if no references to it exist, this
is a mark that no one is interested in the control, let
alone the events it had subscriptions for.
Thanks,
joe