AnsweredAssumed Answered

PiServer Open connection

Question asked by andyromero on May 21, 2015
Latest reply on May 22, 2015 by dng

I´m trying to obtain snapshot values for certain tags.

I want to execute a number of threds, and inside each thread open a connection and get the value and close the connection.

When i execute the simple demo program to test, i get errors relating to close a connection that was not opened.

It seems that when a get a connection  the connection is the same or reused for each thread.

When i execute piServer.Open. Is a new connection created or not?

 

Here is the code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


namespace PIAPP
{
    class TestPIAccess
    {


        #region variáveis privadas
        private const String PASS_FORMAT = "PWD";
        private const String USER_FORMAT = "UID";
        private const String ROLE_FORMAT = "ServerRole=Any";
        #endregion


       
        static void Main(string[] args)
        {
            TestPIAccess t = new TestPIAccess();
            while (true) t.execute(); ;
        }




        public void execute()
        {


            List<string> tags = new List<string>();
            tags.Add("TAGA");
            tags.Add("TABG");
            tags.Add("TAGC");
            tags.Add("TAGD");
            tags.Add("TAGE");

            ManualResetEvent[] resetEvents = new ManualResetEvent[tags.Count];
            for (int i = 0; i < tags.Count; i++)
            {
                ManualResetEvent resetEvent = new ManualResetEvent(false);
                Object[] arr = new Object[3];
                arr[0] = tags[i];
                arr[1] = resetEvent;
                arr[2] = i + 1;
                resetEvents[i] = resetEvent;
                ThreadPool.QueueUserWorkItem(new WaitCallback(threadJob), (object) arr);
            }
            WaitHandle.WaitAll(resetEvents);
        }


        private void threadJob(object o)
        {
            Object[] arr = (Object[])o;
            string tag = (string) arr[0];
            string measuredValue = getValueFromPI( tag );
            Console.WriteLine("Thread DoJobWork(" + (int)arr[2] + ") processed tag: " + tag + ". Value measured:  " + measuredValue);
            ManualResetEvent resetEvent = (ManualResetEvent) arr[1];
            resetEvent.Set();
        }


        private String generateConnectionString(String user, String passwd)
        {
            String conn = USER_FORMAT + "=" + user + ";" + PASS_FORMAT + "=" + passwd + ";" + ROLE_FORMAT;
            return conn;
        }


        private String getValueFromPI(string tagName)
        {
            //Get PI Archive value using Client Time Zone
            PISDK.PISDK mySDK = new PISDK.PISDK();
            PISDK.Server piServer = mySDK.Servers["MYSERVER"];
            piServer.Open(generateConnectionString("USER", "PASS"));
            PISDK.PIPoint point = piServer.PIPoints[tagName];
            PISDK.PIValue value = point.Data.Snapshot;
            string measuredValue;
            if (point.PointType == PISDK.PointTypeConstants.pttypDigital)
            {
                PISDK.DigitalState digitalState = (PISDK.DigitalState)value.Value;
                PITimeServer.PITime timeStamp = value.TimeStamp;
                measuredValue = timeStamp.LocalDate + " : " + digitalState.Name;
            }
            else
            {
                String arcValue = value.Value.ToString();
                PITimeServer.PITime timeStamp = value.TimeStamp;
                measuredValue = timeStamp.LocalDate + " : " + arcValue;
            }
            piServer.Close();
            return measuredValue;
        }


    }
}

 

i´ve atached a screenshot with the exception.

 

Thanks in advance for any help.

Attachments

Outcomes