Author Topic: C# Data Subscriber in a non UI class  (Read 1188 times)

TheColonel26

  • Newbie
  • *
  • Posts: 25
    • View Profile
C# Data Subscriber in a non UI class
« on: September 29, 2016, 10:46:55 AM »
Code: [Select]

public partial class Main : Form
    {
       
        public Main()
        {
            InitializeComponent();


            Program.Logic.Initialize(omronEthernetFINSCom1);
            Program.Logic.DataSubFixtureID.DataChanged += DataSubFixtureID_DataChanged;
            Program.Logic.SequenceDone += Logic_SequenceDone;
        }

        private void Logic_SequenceDone(object sender, Vitrek_Library.MainSequenceDoneEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void DataSubFixtureID_DataChanged(object sender,
            MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e)
        {

            int fixtureId = Convert.ToInt32(e.Values[0]);
            lblPartNumber.Text = $@"Part Number: {fixtureId}";
        }
    }

public class Logic{
        public DataSubscriber DataSubFixtureID;

        public void Initialize(IComComponent comComponent)
        {
            data = new DataInterface();
            data.Load();

            SeqRight = new Sequences();
            SeqRight.SequenceDone += SeqRight_SequenceDone;
            SeqRight.Initialize(new System.IO.Ports.SerialPort());
            SeqLeft = new Sequences();
            SeqLeft.SequenceDone += SeqLeft_SequenceDone;
            SeqLeft.Initialize(new System.IO.Ports.SerialPort());


            DataSubFixtureID = new DataSubscriber
            {
                PLCAddressValue = new PLCAddressItem("D10"),
                ComComponent = comComponent
            };

        }
}

I get no errors but the DataChanged Event is never fired.

I have a DigitalPanelMeter using the same comComponent using the same address and it works just fine.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5270
    • View Profile
    • AdvancedHMI
Re: C# Data Subscriber in a non UI class
« Reply #1 on: September 29, 2016, 11:54:09 AM »
The DataSubscriber implement ISupportInitialize for proper subscribing. When creating a DataSubscriber in code, be sure to call BeginInit() before setting properties and then calling EndInit() afterwards.
« Last Edit: October 05, 2016, 07:41:12 AM by Archie »

TheColonel26

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: C# Data Subscriber in a non UI class
« Reply #2 on: October 05, 2016, 07:09:02 AM »
Just realized I never replied back. This fixed it thank you.