Click here to Skip to main content
16,020,111 members
Home / Discussions / C#
   

C#

 
GeneralRe: TreeView in C# Pin
Henry Minute23-Oct-09 9:01
Henry Minute23-Oct-09 9:01 
GeneralRe: TreeView in C# Pin
silva10323-Oct-09 16:44
silva10323-Oct-09 16:44 
QuestionString split Pin
Sebastian T Xavier23-Oct-09 1:52
Sebastian T Xavier23-Oct-09 1:52 
AnswerRe: String split Pin
dan!sh 23-Oct-09 2:02
professional dan!sh 23-Oct-09 2:02 
AnswerRe: String split [modified] Pin
musefan23-Oct-09 2:42
musefan23-Oct-09 2:42 
GeneralRe: String split Pin
Sebastian T Xavier23-Oct-09 3:23
Sebastian T Xavier23-Oct-09 3:23 
GeneralRe: String split Pin
J4amieC23-Oct-09 4:09
J4amieC23-Oct-09 4:09 
QuestionDestructor peculiarity Pin
DaveyM6923-Oct-09 1:25
professionalDaveyM6923-Oct-09 1:25 
I have a manager class that is responsible for controlling a whole bunch of ports. Each port can only exist once so it's constructor is internal and used by the manager class only. This is a class library so I have no control how it will be used in any consuming application. It is critical that every port is closed at the end of the application so I have provided a CloseAll method. I can implement IDisposable but if Dispose or CloseAll isn't called it could have severe consequences that I'd like to avoid instead of saying you MUST call one of these methods.

The obvious solution is to use the destructor to call CloseAll or Dispose. I have experimented with various implementations using a static manager class, making the manager class a Singleton etc but none were reliable, I assume because the order of destruction of objects cannot be predicted, so CloseAll could be trying to close port instances that have already been GCed.

The sketch code below however seems to work 100% reliably (I have tried this in the full 'real' code with no problems too) and the destructor always gets called - and before any ports are collected. Perfect - but what I can't work out is why! Can anyone shed any light?
C#
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Manager.Ports[0].Open();
    }
}

public class Manager
{
    private static List<Port> ports;
    private static Manager manager = new Manager();

    static Manager()
    {
        Initialize();
    }
    private Manager()
    { }

    ~Manager()
    {
        CloseAll();
    }

    public static IList<Port> Ports
    {
        get { return ports.AsReadOnly(); }
    }

    public static void CloseAll()
    {
        foreach (Port port in ports)
            port.Close();
    }

    private static void Initialize()
    {
        ports = new List<Port>(1);
        ports.Add(new Port());
    }
}

public class Port
{
    internal Port() { }
    public void Close()
    {
    }
    public void Open()
    {
    }
}


Dave

Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

AnswerRe: Destructor peculiarity Pin
Pete O'Hanlon23-Oct-09 1:38
mvePete O'Hanlon23-Oct-09 1:38 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 2:20
professionalDaveyM6923-Oct-09 2:20 
GeneralRe: Destructor peculiarity Pin
Pete O'Hanlon23-Oct-09 2:25
mvePete O'Hanlon23-Oct-09 2:25 
GeneralRe: Destructor peculiarity Pin
Gideon Engelberth23-Oct-09 7:47
Gideon Engelberth23-Oct-09 7:47 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:44
professionalDaveyM6923-Oct-09 9:44 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:52
professionalDaveyM6923-Oct-09 9:52 
GeneralRe: Destructor peculiarity Pin
cmk23-Oct-09 5:21
cmk23-Oct-09 5:21 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 8:10
professionalDaveyM6923-Oct-09 8:10 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:48
professionalDaveyM6923-Oct-09 9:48 
AnswerRe: Destructor peculiarity Pin
Gideon Engelberth23-Oct-09 7:55
Gideon Engelberth23-Oct-09 7:55 
GeneralRe: Destructor peculiarity Pin
DaveyM6923-Oct-09 9:40
professionalDaveyM6923-Oct-09 9:40 
AnswerRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 3:36
sitebuilderLuc Pattyn27-Oct-09 3:36 
GeneralRe: Destructor peculiarity Pin
DaveyM6927-Oct-09 7:04
professionalDaveyM6927-Oct-09 7:04 
GeneralRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 7:09
sitebuilderLuc Pattyn27-Oct-09 7:09 
GeneralRe: Destructor peculiarity Pin
DaveyM6927-Oct-09 10:45
professionalDaveyM6927-Oct-09 10:45 
GeneralRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 11:17
sitebuilderLuc Pattyn27-Oct-09 11:17 
GeneralRe: Destructor progress report 1 Pin
Luc Pattyn27-Oct-09 17:20
sitebuilderLuc Pattyn27-Oct-09 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.