It is of course placed inside a while loop.

I get no errors. The dragging is just not synchronized. When I move the mouse, the cursor is at another position according to the screen image after the drag.