I have a thread that does the following:
1) Do some work
2) Wait
3) Do some more work
4) Wait
…
I want to (be able to) interrupt the thread while in one of the sleep sections but not in the work sections. The problem is that a work section might contain a smaller sleep section which might then catch an interrupt. So what I need is some way to prevent interrupt within a certain section, how can I do this?
Try to use some object to synchronize thread interrupting.
In your thread, use lock statement for work sections:
When you need to interrupt your thread, also do this inside lock statement: