Deleting ones self from a Dictionary



Assuming a class contains an event handler (say from a timer), and -
upon the event being caught - deletes itself from a dictionary, is this
legal? Will the GC ever clean this up (a breakpoint on the Dispose
method below never gets called, and a memory profile seems to show the
GC age is always zero).

The following code sample illustrates this scenario:
public class foo {
public static Dictionary<string,Bar> barDict;
...
public void CreateBar( string ID )
{
barDict.Add(ID, new Bar(ID));
}
}
public class Bar : IDisposable
{
public string key;
private Timer myTimer = new Timer();;
public Bar(string id)
{
key = id;
myTimer.Interval = 2000; myTimer.AutoReset = false;
myTimer.Elapsed += new
System.Timers.ElapsedEventHandler(timeout_Elapsed);
myTimer.Start();
}
void timeout_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
foo.barDict.Remove(key);
}
public void Dispose()
{
}
}

.



Relevant Pages

  • Re: subclass JPasswordField
    ... for (like how to add a document change listener to the PlainDocument ... inside the JPasswordField & how to set the timer and all). ... Google "java timer tutorial". ... public void insertUpdate{ ...
    (comp.lang.java.programmer)
  • [patch 15/21] clockevents: drivers for i386
    ... lapic and PIT. ... Update the timer IRQ to call into the PIT driver's event handler ... * of the boot CPU and register the clock event in the framework. ...
    (Linux-Kernel)
  • [patch 15/22] clockevents: drivers for i386
    ... lapic and PIT. ... timer IRQ to call into the PIT driver's event handler and the lapic-timer IRQ ... * of the boot CPU and register the clock event in the framework. ...
    (Linux-Kernel)
  • Re: Help with design
    ... If so, put a timer in Session, ... and set up an event handler for the timer's Elapsed event that does what you ... If it is for all users, put the timer in Application Cache. ... Now you want to know how to modify that Control to do what you ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Pocket outlook - AppointmentsCollection class: ListChanged event
    ... void POOMHelper_OutlookItemChanged(PIMNotificationMessage notification, PIMItemType itemType, int itemOID, int databaseOID) ... static Timer timer= new Timer; ... Thread timerThread = timer.Start; ... public void SleepIntermittently ...
    (microsoft.public.dotnet.framework.compactframework)