Improve UI loop handling.

Create and destroy the UI refresh loop when needed.