Deleting ones self from a Dictionary
- From: tjohnson@xxxxxxxxxxxx
- Date: 26 Oct 2006 12:40:14 -0700
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()
{
}
}
.
- Follow-Ups:
- Re: Deleting ones self from a Dictionary
- From: Brian Gideon
- Re: Deleting ones self from a Dictionary
- Prev by Date: Re: compie error CS0117
- Next by Date: Re: Interface from c# to c++ problem with IntPtr type
- Previous by thread: Re: Inconsistent accessiblity compiler error when putting public metho
- Next by thread: Re: Deleting ones self from a Dictionary
- Index(es):
Relevant Pages
|