Extracted task creation and lock handling to its own method.