Leonardo78

STA/MTA issues

Discussion created by Leonardo78 on Apr 7, 2014
Latest reply on Apr 11, 2014 by Leonardo78

Hello guys,

 

I've got a typical STA/MTA issue with this very simple code to handle eventpipe events.

 

 

 
        //[STAThread]
        static void Main(string[] args)
        {
            PISDK.PISDK piSdk = new PISDK.PISDK();
            Server piServer = piSdk.Servers.DefaultServer;
            piServer.Open();
            PointList piPoints = new PointList();
            piPoints.Add(piServer.GetPoints("Tag='analog_p0'")[1]);
            eventPipe = (piPoints.Data.EventPipe as IEventPipe2);
            _DEventPipeEvents_Event epeEvent = (PISDK._DEventPipeEvents_Event)eventPipe;
            epeEvent.OnNewValue += new _DEventPipeEvents_OnNewValueEventHandler(EventPipeEventOnNewValue);
            Thread.Sleep(10000);
            PointList piPoints2 = new PointList();
            piPoints2.Add(piServer.GetPoints("Tag='analog_p1'")[1]);
            eventPipe.AddSignUp(piPoints2, null);
            Console.WriteLine("Press Enter to quit.");
            Console.ReadLine();
        }

        private static void EventPipeEventOnNewValue()
        {
            PointValue pointValue;
            Array events = eventPipe.TakeAll();
            foreach (PIEventObject evt in events)
                {
                    pointValue = evt.EventData;
                    Console.WriteLine("EVENT: {0} {1}.", pointValue.PIPoint.Name, pointValue.PIValue.Value);
                }
            }

 

 

 As you can foresee, this code will:

  1. crash on the AddSignup method if [STAThread] attribute is commented.
  2. not report any new event in the pipe if [STAThread] attribute is not commented.
Any clue about how to fix the AddSignUp() crash (external component throw and exception) and, at the same time, let the event raise correctly?
PS: this exact code works fine in a WPF application but not in a Console, nor in a Windows Service.
Thanks. 

Outcomes