Scriptjob Problems

So i'm having a problem with a scriptjob that don't want to be killed.

I have a scriptJob (letts call it deleteJob) that runs a procedure when my custom node is deleted.

That procedure then deletes a scriptNode that runs another procedur when deleted.

That procedure is supposed to kill all scriptjobs that was connected to the custom node.

Everything works fine up until the point when it's time to delete the scripjob called deleteJob. For some reason Maya thinks it's still being used and prints out: Error: A scriptJob cannot be killed while it is running.

So i was wondering if anybody know why Maya does this and if there is a solution to my problem. l

You may use "evalDeferred" to invoke the command to kill your scriptJob. This will defer the execution of the command until Maya is idle, and thus will be outside of the scope of your scriptJob's execution.

Because you are deferring the commands, you should take extra care within the deferred commands to verify that Maya is still in the state you expect. Check for the existence of items that you are about to delete rather than just blindly deleting them.