TL;DR version: I have two threads. One of them might need to Interrupt() the other, but only if the other thread is in th middle of processing data that is related to the object that is affected by the first thread. How can I only Interrupt() the second thread based on certain conditions.
I have been working on a program that spins up two threads for processing socket data and updating the GUI with the information. The two threads (topologyThread and dataThread) handle two different aspects of my application, namely topology change notifications for our wireless sensor network, and data information for receiving and processing data from the wireless sensor network.
When data is coming into the dataThread, it's possible that the network member that the data packet represents has already been removed by the topologyThread and as such should not be processed. So, I was thinking that using the Thread.Interrupt() method would allow me to instantly notify the dataThread of changes to the topology and prevent some of the problems we have seen with trying to update the GUI with data for a network member that is no longer connected. My question is this: How can I tell if the dataThread needs to be interrupted? I don't want it to throw an exception while it's processing data for a network member that is still connected, but I want it to be interrupted if it's in the middle of processing data for a network member that is then disconnected.
Scenario 1:
- Data comes in on dataThread.
- dataThread makes sure network member is still part of the network.
- dataThread processes data to the end and updates GUI
Scenario 2:
- Data comes in on dataThread.
- dataThread makes sure network member is still part of the network.
- Network member has been disconnected so no processing takes place.
Scenario 3:
- Data comes in on dataThread.
- dataThread makes sure network member is still part of the network.
- dataThread begins processing data
- topologyThread receives notice that the network member is disconnected and removes it from topology.
- dataThread tries to update GUI for a network member that is no longer connected.
It's scenario 3 that I am trying to code for. In our testing, this scenario is where the tree view in the sidebar freezes up and the app has to be killed. But I only need to interrupt the dataThread if the object that is affected by the topologyThread is the one that the dataThread is currently acting on.
Thanks for reading. :)