How to get the instance of Gadgeteer Program "Singleton"

Hello guys,
i was wondering if there is any way to get the instance of program, because Gadgeteer builds singleton apps and doesn’t give us the reference of the instance to work with therefore i can’t create a new instance hince Singleton so the only way i can get the isntance righ now is by Modifiying the Program.generated .cs file which is a no no since that can be and will be overriden when i make a change in the designer so my question is how can i get hold of the instance… and in case you are wondering why i need that is simply to be able to call non-static method from a static member.

current generated code :


        public static void Main()
        {
            //Important to initialize the Mainboard first
            Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();

            Program program = new Program();
            program.InitializeModules();
            program.ProgramStarted();
            program.Run(); // Starts Dispatcher
        }

to make it work i do this


         //*******************Added this**********************************
        private static Program program = null;
        public static Program Instance
        {
            get
            {
                return program;
            }
        }
        //***************************************************************

        public static void Main()
        {
            //Important to initialize the Mainboard first
            Mainboard = new GHIElectronics.Gadgeteer.FEZSpider();

            //*******************Updated this****************************
            program = new Program();
            //***********************************************************

            program.InitializeModules();
            program.ProgramStarted();
            program.Run(); // Starts Dispatcher
        }

and here is why i need it: in Program.cs i can do this


        public static void DoSomeWork(string relay) //static method can be access from other classes
        {
            Instance.Trigger(relay); //call the non static method.
        }
        public void Trigger(string relay) //non-static method that can access Gadgeteer modules.
        {
            if (relay == "relay1") relays.Relay1 = true;
            if (relay == "relay2") relays.Relay2 = true;
        }

is there away to change how the code is generated and the extra required code? or is there another way to do this?

thanks.

There is another way.

Add it into your non-generated Program.cs


        private static Program Instance;
        void ProgramStarted()
        {
            Instance = this;
            /******************************************************************************************
            Access modules defined in the designer by typing their name:                            
            
            e.g.  button
                  camera1

            Initialize event handlers here.
            e.g. button.ButtonPressed += new GTM.MSR.Button.ButtonEventHandler(button_ButtonPressed);             
            ***************************************************************************************** */

            // Do one-time tasks here
            Debug.Print("Program Started");

            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
            sdCard.MountSDCard();
        }

You would do this?

In my opinion this is going to lead to a very bad designed program…

I think you should just write a small RelayHandler for this.
Initialize it in Program.cs and use it wherever you like.

The handler looks like this:


public static class RelaysHandler
    {
        private static Relays _relays;
        public static void Initialize(Relays relays)
        {
            _relays = relays;
        }

        public static void Trigger(string relay) //non-static method that can access Gadgeteer modules.
        {
            if(_relays == null)
                throw  new InvalidOperationException("You have to call Initialize first!");

            if (relay == "relay1") _relays.Relay1 = true;
            if (relay == "relay2") _relays.Relay2 = true;
        }
    }

The call in Program.cs:

RelaysHandler.Initialize(relays);

Now the Trigger method can be called from everywhere…

Thank you Both,
@ Architect: i realized that i could reference the instance afterwords, sometimes when you are trying too hard you miss simple things like that…
but I’m glad i kept the post here because i like Weiti Solution, much more elegant solution for sure.

so thank you.

Jay.

To be honest I have completely missed second part of the question about relays. ;D Multitasking ::slight_smile: