Resolved How to close/delete/dispose of an object

blinky

Member
Joined
Oct 21, 2020
Messages
7
Programming Experience
Beginner
I have a class that creates and opens a serial port:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ObservatoryControl
{
    public static class ObservatoryPortProvider

    {

        private static SerialPort ObservatoryCompPort = null;

        public static  SerialPort GetObservatoryComPort(string SelectedComPort)

        {

            if (ObservatoryCompPort == null)
            {

                ObservatoryCompPort = new SerialPort();

                ObservatoryCompPort.PortName = SelectedComPort;

                ObservatoryCompPort.Open();

            }

            return ObservatoryCompPort;

        }

        public static void CloseObservatoryComPort()

        {

            if (ObservatoryCompPort != null)
            {

                ObservatoryCompPort.close(); //I'm just guessing there is close method

                ObservatoryCompPort = null;

            }

        }


    }
}

I then call the GetObservatoryComPort method to create the object:
C#:
 private void BtnConnect_Click(object sender, EventArgs e)
        {
            string SelectedComPort = (string)ListBoxSerialPorts.SelectedItem;
            ObservatoryComPort = ObservatoryPortProvider.GetObservatoryComPort(SelectedComPort);

          
            if (ObservatoryComPort.IsOpen)            {
                
                MyTimer.Interval = 1000;
                MyTimer.Tick += new EventHandler(MyTimer_Tick);
                MyTimer.Start();               

            }
                

        }

The problem I have is that if the user clicks the connect button again, I create 2 instances of the ObservatoryComPort - how can I stop this happening? I want something like if ObservatoryComPort exists!
 

Attachments

  • 1603369323270.png
    1603369323270.png
    50.9 KB · Views: 26
Figured it out - I found out that what I was actually doing was creating a second timer object which was populating a list box, this meant it was populating twice 1 for each timer and not creating a 2nd port!
 
I would caution against using a singleton static member like your ObservatoryPortProvider.ObservatoryComPort. As you observered if your provider's ObservatoryPortProvider.GetObservatoryComPort() gets called multiple times, only the last one will be closed by a call to ObservatoryPortProvider.CloseComPort. Presumably you would need one COM port to control the azimuth of the dome, and another one to control the opening/closing of the shutters, and one or more COM ports to control the azimuth and altitude of the telescope.

I would also caution against the Smurf naming convention that you are falling into.
 
The following is just me trying to de-stress and having fun. It's not meant to drive you down a cattle shoot for how to design things. It's just me doing a "drawing on the back of a napkin" of how I would approach a mostly domain driven design for controlling the various serial ports.
Screenshot_1.png
 
Thanks for the replies - Im going to make the changes you request, well to the naming convention at least, as for the serial port, I will only ever have 1 serial port. I am communicating with an Arduino and it will control the switch to open/shut the observatory roof.
 

Attachments

  • Ascom Choooser.JPG
    Ascom Choooser.JPG
    44.4 KB · Views: 33
  • Ascom simulator.JPG
    Ascom simulator.JPG
    48.7 KB · Views: 34
Good to hear that. Looks like a fun project.
 
Back
Top Bottom