You don't have to be in safe mode, and you don't have to close any running programs.
However, if you have programs accessing the disk while you are trying to defrag, that will keep interrupting the defrag process, and it will keep starting over. So, the best bet is to stop all running applications. If you're still having problems with constant restarts, you probably are running some services or other programs that start automatically when you boot your system or login to your system. In that case, running it in safe mode should take care of the restarts.
There are 3rd party disk defragmenter programs that don't suffer from the problems of constant restarts, and don't require as much free disk space as the one that comes with Windows XP. It's your call if you think it's worth buying one of them or not. I like diskeeper, however, I'm really annoyed by their constant emails asking me to upgrade. On the other hand, it has some sort of smart IO feature, which basically pauses the defrag process when anything else is accessing the disk, then it resumes from where it left off, so if you enable this feature there are no restarts and it can run while you are using the computer for anything else.
-David