Why does this program sometimes crash, and sometimes not?
Perhaps you've already seen this but the gist of this article by Windows god Raymond Chen is that using HWND_BROADCAST in this way is not recommended. I found this via comments on a site that shall not be named, to the effect that the sample code you are using here, while popular, is not the right way to handle monitor powerdown.
This does not explain why you are seeing exactly what you are seeing, but it does offer some evidence that the code in question is suspect.