Fixing background task bug when app is minimized and maximied quickly a few times causing a dangling task to never be ended