I'm not able to see an issue with the piece of code that you have pasted here. Maybe you could open a bug report with halcust attached and repeatable situation that I can look at?
I would like to know what BackgroundHALShouldPause does, and how to use it. While it's listed in the HAL Primitives report, it is not used in source code and it doesn't appear in the LMS HAL...