Question

Using the communication Library for Kepware OPC Server v4.0 (OPCServerCommLib.dll) I started receiving this error when I try to disconnect from the OPC server. Information about this error is available from nowhere and Kepware support was unable to help. I think that my program must still be trying to read even after I perform the disconnect operation but I don't understand why. Here is basically how I am reading data using a thread:

Private Sub MonitoringThread()
  If OPCServerConnected Then
  PerformOPCReadOperation()
  End If
System.Threading.Thread.Sleep(100)
End Sub
Était-ce utile?

La solution

Even though OPCServerConnected is made false upon disconnect, this variable isn't getting updated in time for the tread to see it. This is how I resolved the issue. If anyone can see a better way to do this, feel free to post your own answer.

Add this line just before the end of the thread: ThreadStopped()

In the base class, add the following:
Public m_AutoResetEvent As AutoResetEvent = New AutoResetEvent(False)

Protected Sub ThreadStopped()
    m_AutoResetEvent.Set()
End Sub

Finally, in routines where disconnect is called for (Shutdown, Exit, Restart, etc.), add the following line before calling the OPC Server disconnect method:

m_AutoResetEvent.WaitOne() 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top