I would guess there is some problem with how autoit handles messages or there is some problems with your script being tied to handle one thing when you would rather handle something else... ?
The quick and dirty fix, (Not so dirty, but not the elegant "wisdom-enhancing" solution.) is to make a separate program for just dealing with stopping shutdown or similar, and do everything else in first program, communicating between them or starting/stopping as needed.
We would like no visible GUI and NO trayicon:
#NoTrayIcon
/Manko