ContextSwitchDeadlock

Comments & issues
Post Reply
AndyG
Fresh meat
Posts: 1
Joined: Tue Jun 28, 2022 1:36 pm
Contact:

ContextSwitchDeadlock

Post by AndyG »

Hi I'm receive this error message, about a minute after I start my Phidget system...Any ideas, I need to resolve this asap.

ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x23f45f8 to COM context 0x23f46b0 for 60 seconds.
The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a
very long running operation without pumping Windows messages.
This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time.
To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles)
and routinely pump messages during long running operations.


I'm Using VB2005 on Windows 10 and it appears to only happen if I have the encoder enabled.

Briefly the code contains...

Imports Phidget22
Imports Phidget22.Events

Module SysM_Phidgets
'Define Installed Phidgets
Public WithEvents pdgEncoder_ZPOSITION As New Encoder
Public WithEvents pdgInput_ZDATUM As New DigitalInput
Public WithEvents pdgInput_ZSHELF As New DigitalInput
Public pdgOutput_ZUp As New DigitalOutput
Public pdgOutput_ZDown As New DigitalOutput
Public serialNo As Integer

Sub InitialisePhidgetSystem()
If NO_PHIDGETS Then Exit Sub

Select Case gtCalSys.SiteRefCode
Case SiteRef.AX1 : serialNo = 618618 'AWE X-Ray Sys
Case SiteRef.AN1 : serialNo = 618312 'AWE Neutron Sys
Case SiteRef.SG1 : serialNo = 596699 'Singapore Gamma Sys
Case Else : serialNo = 596735 'Nuvia Test Sys
End Select

Try
Net.EnableServerDiscovery(ServerType.Device) 'turn on network scan

With pdgEncoder_ZPOSITION 'Z Axis (Up/Down) height encoder
.DeviceSerialNumber = serialNo
.IsHubPortDevice = False
.IsRemote = True
.HubPort = 0
.Channel = 0
.Open(5000)
If Not .Attached Then
MsgBox("Phidget Encoder Module not attached - Z Axis", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "NuCal Hardware Error")
Else
.IOMode = EncoderIOMode.OpenCollector_10K
.DataInterval = 250
.Position = 0
.PositionChangeTrigger = 10
.Enabled = True
End If

End With

With pdgInput_ZDATUM 'Z Axis (Up/Down) top switch
.DeviceSerialNumber = serialNo
.IsHubPortDevice = False
.IsRemote = True
.HubPort = 1
.Channel = 0
.Open(1000)
If Not .Attached Then MsgBox("Z Axis Datum - Input Module not attached", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "NuCal Hardware Error")
End With

With pdgInput_ZSHELF 'Z Axis Shelf fitted switch
.DeviceSerialNumber = serialNo
.IsHubPortDevice = False
.IsRemote = True
.HubPort = 1
.Channel = 1
.Open(1000)
If Not .Attached Then MsgBox("Z Axis Shelf - Input Module not attached", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "NuCal Hardware Error")
End With

With pdgOutput_ZUp 'Z Axis (Up) control relay
.DeviceSerialNumber = serialNo
.IsHubPortDevice = False
.IsRemote = True
.HubPort = 2
.Channel = 0
.Open(1000)
If Not .Attached Then MsgBox("Z Axis Up motor - Output Module not attached", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "NuCal Hardware Error")
.State = False
End With

With pdgOutput_ZDown 'Z Axis (Down) control relay
.DeviceSerialNumber = serialNo
.IsHubPortDevice = False
.IsRemote = True
.HubPort = 2
.Channel = 1
.Open(1000)
If Not .Attached Then MsgBox("Z Axis Down motor - Output Module not attached", MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "NuCal Hardware Error")
.State = False
End With

Exit Try

Catch ex As PhidgetException
MsgBox("Error initializing Phidget System: " + ex.Message)
End Try

Phidget22.Phidget.InvokeEventCallbacks = True

' Get currents states
If pdgInput_ZSHELF.Attached Then gbSHELF = pdgInput_ZSHELF.State
If pdgInput_ZDATUM.Attached Then gbZ_DATUM = pdgInput_ZDATUM.State

End Sub

Public Sub ClosePhidgetSystem()
If NO_PHIDGETS Then Exit Sub
If pdgOutput_ZUp.Attached Then pdgOutput_ZUp.Close() : pdgOutput_ZUp = Nothing
If pdgOutput_ZDown.Attached Then pdgOutput_ZDown.Close() : pdgOutput_ZDown = Nothing
If pdgInput_ZDATUM.Attached Then pdgInput_ZDATUM.Close() : pdgInput_ZDATUM = Nothing
If pdgInput_ZSHELF.Attached Then pdgInput_ZSHELF.Close() : pdgInput_ZSHELF = Nothing
If pdgEncoder_ZPOSITION.Attached Then pdgEncoder_ZPOSITION.Close() : pdgEncoder_ZPOSITION = Nothing
End Sub

Private Sub Encoder_Error(ByVal sender As Object, ByVal e As Phidget22.Events.ErrorEventArgs) Handles pdgEncoder_ZPOSITION.Error
MsgBox(e.Description, MsgBoxStyle.OkOnly, "NuCal Z-Axis Encoder Error")
End Sub

Private Sub Encoder_Change(ByVal sender As Object, ByVal e As Phidget22.Events.EncoderPositionChangeEventArgs) Handles pdgEncoder_ZPOSITION.PositionChange
Try
DisplayPosition(Axis.Z, CDbl(pdgEncoder_ZPOSITION.Position / 15.64)) 'Convert ticks to mm

Catch ex As Exception
MsgBox("Error reading Z position: " + ex.Message)
End Try
End Sub

End Module
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests