public OpcRequest Remove()
        {
            mtx.WaitOne();
            OpcRequest req = (OpcRequest)reqQueue.Dequeue();

            mtx.ReleaseMutex();
            return(req);
        }
        //-----------------------------------------------------
        // Thread that dequeues the requests and makes the OPC server access
        private void OpcAccessThread()
        {
            for (; ;)         // Thread loop
            {
                if (Requests.Count() > 0)
                {
                    OpcRequest req = Requests.Remove();

                    // An item value is read and displayed in the defined TextBox.
                    if (req.Cmd == Command.Read)
                    {
                        OPCItemState val;
                        int          rtc = SioGrp.Read(OPCDATASOURCE.OPC_DS_CACHE, req.ItemID, out val);
                        if (HRESULTS.Failed(rtc))
                        {
                            Console.WriteLine("Read failed with error 0x" + rtc.ToString("X"));
                        }
                        else
                        {
                            if (HRESULTS.Failed(val.Error))
                            {
                                Console.WriteLine("Read failed with error 0x" + val.Error.ToString("X"));
                            }
                            else
                            {
                                Console.WriteLine(val.DataValue.ToString(), req.TxtBox);
                            }
                        }
                    }

                    // A value is written to the OPC server item
                    else if (req.Cmd == Command.Write)
                    {
                        int rtc = SioGrp.Write(req.ItemID, req.Val);
                        if (HRESULTS.Failed(rtc))
                        {
                            Console.WriteLine("Write failed with error 0x" + rtc.ToString("X"));
                        }
                    }
                }

                Thread.Sleep(100);      // ms

                if (StopThread != null) // Thread kill request
                {
                    StopThread.Set();
                    return;               // terminate the thread
                }
            }
        }
        //---------------------------------------------------------------
        // Queue OPC server access request
        public void Request(OpcRequest req)
        {
            if (SioGrp == null)
            {
                return;
            }

            try
            {
                int rtc = SioGrp.Write(req.ItemID, req.Val);
                if (HRESULTS.Failed(rtc))
                {
                    Console.WriteLine("Write failed with error 0x" + rtc.ToString("X"));
                }
            }
            catch (Exception errmsg)
            {
            }
            //Requests.Add(req);
        }
 public void Add(OpcRequest req)
 {
     mtx.WaitOne();
     reqQueue.Enqueue(req);
     mtx.ReleaseMutex();
 }