Example #1
0
        public bool Disconnect()
        {
            var flag = false;

            try
            {
                if (mItemIDList != null)
                {
                    var handlesSrv = HandleClients.Keys.ToArray <int>();
                    if (TheGrp != null)
                    {
                        int[] aE;
                        TheGrp.RemoveItems(handlesSrv, out aE);
                        TheGrp.DataChanged    -= TheGrp_DataChanged;
                        TheGrp.ReadCompleted  -= TheGrp_ReadCompleted;
                        TheGrp.WriteCompleted -= TheGrp_WriteCompleted;
                        TheGrp.Remove(false);
                        TheGrp = null;
                    }
                }
                if (TheSrv != null)
                {
                    TheSrv.Disconnect();
                    flag   = true;
                    TheSrv = null;
                }
            }
            finally
            {
                TheSrv = null;
                TheGrp = null;
            }
            return(flag);
        }
Example #2
0
 public bool Connect(params string[] ServerProgID)
 {
     try {
         if (TheSrv == null)
         {
             TheSrv = new OpcServer();
             if (ServerProgID.Length == 0)
             {
                 var s = ServerList;
                 if (s.Count > 0)
                 {
                     TheSrv.Connect(s[0]);
                 }
                 else
                 {
                     return(false);
                 }
             }
             else
             {
                 TheSrv.Connect(ServerProgID[0]);
             }
         }
         else
         {
             var a = new SERVERSTATUS();
             TheSrv.GetStatus(out a);
             if (a.eServerState != OPCSERVERSTATE.OPC_STATUS_RUNNING)
             {
                 if (ServerProgID.Length == 0)
                 {
                     var s = ServerList;
                     if (s.Count > 0)
                     {
                         TheSrv.Connect(s[0]);
                     }
                 }
                 else
                 {
                     TheSrv.Connect(ServerProgID[0]);
                 }
             }
         }
         return(true);
     } catch (Exception) {
         TheSrv.Disconnect();
         throw;
     }
 }