Can't tell from what you posted, the loop runs until res2=0 and res2 is not being changed in the loop so it should run forever i.e. infinite loop

What does CancelTo() do? I would suspect there...