My teacher always described the difference by talking about a camera. If the film was still rolling , eg I was eating when... Then it is the imperfect. If it is a photo that has been taken, ie the action has ended then it is the perfect, eg I ate a cake - I am not still eating it, the action has ended.
Hope that helps a bit!