public OpcRequest Remove() { mtx.WaitOne(); OpcRequest req = (OpcRequest)reqQueue.Dequeue(); mtx.ReleaseMutex(); return(req); }
//--------------------------------------------------------------- // Queue OPC server access request public void Request(OpcRequest req) { if (SioGrp == null) { return; } Requests.Add(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) && !HRESULTS.Failed(val.Error)) { req.res = true; req.Val = val.DataValue.ToString(); } } // 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)) { req.res = true; } } } Thread.Sleep(100); // ms if (StopThread != null) // Thread kill request { StopThread.Set(); return; // terminate the thread } } }
public void Add(OpcRequest req) { mtx.WaitOne(); reqQueue.Enqueue(req); mtx.ReleaseMutex(); }