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.



        static void Main(string[] args)
            PISDK.PISDK piSdk = new PISDK.PISDK();
            Server piServer = piSdk.Servers.DefaultServer;
            PointList piPoints = new PointList();
            eventPipe = (piPoints.Data.EventPipe as IEventPipe2);
            _DEventPipeEvents_Event epeEvent = (PISDK._DEventPipeEvents_Event)eventPipe;
            epeEvent.OnNewValue += new _DEventPipeEvents_OnNewValueEventHandler(EventPipeEventOnNewValue);
            PointList piPoints2 = new PointList();
            eventPipe.AddSignUp(piPoints2, null);
            Console.WriteLine("Press Enter to quit.");

        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.