I have an C# application by which I configure the parameters of an Ethernet camera. The camera acts as a server and the application acts as client.
There are 2 sockets for communication, one for data/command communication – data socket(2 way communication) and another socket for image, the camera sends the image via the image socket(one way communication). The camera will send the image via image socket whenever a hardware trigger is given to it(not predictable when the hardware trigger would come).
The user can configure the camera by changing the values in the GUI. When user changes a parameter, the applications sends the command and the data to camera via the command socket and waits for response(Receive timeout is 5 sec). The camera receives the command and sends the response back. The GUI receives it updates the user that its successfully updated.
I have put the image socket in the background worker.An infinite while loop inside it keeps polling for image in image socket and whenever the image is received, it is displayed in GUI(I use a delegate to do that). All is well 🙂
Now, when a trigger has come to camera, it would send the image. At the same instant the user changes a parameter, the application sends the command/data and waits for response. The camera will receive the command only after sending the image completely. What happens is that the backgroundworker(that receives the image)is not running when the application is waiting for the response for the command. So, UI is waiting for response in command socket and camera is sending image in image socket and UI does not receive the image as background worker is not running. It becomes a deadlock and timeout(5 sec) happens at the command socket :(.
Why is the backgroundworker not running when the socket is waiting for timeout?
I replaced the background worker with thread and I set the priority as highest. The error (timeout) frequency has come down, but the same problem happens occasionaly. Can any of you guys help me sort this and help me understand the problem.
Thanks,
Vishnu
I would suggest you make sure that the delegate used to refresh the image from the background worker is called asynchronously (using BeginInvoke). Synchronous invocation may cause interlocking.