Alexander,
You cannot create a file that is open. Your first run creates the prg and compiles it to an FXP and then creates an instacne of the class in the fxp. The fxp file is now in use by the first run of the application.
The secodn urn creates the prg and then tries to compile it, but the first run has the fxp open and so the second run cannot create the fxp.