I don't think you are doing anything wrong there. My guess is that this long running task is (perhaps) releasing the file during the process, and the continues again at some point..?
Anyway, since it is a long running task it is maybe better to create the file in a temp folder, and when it is ready, move it to to folder in question (with trigger). Having a long running task in a monitored folder seems error prone to me.
Thomas