When I need to switch the suspension fork, should I stop before switch?
Yes, you should generally stop and set the suspension to an uncompressed/unloaded position before you switch, especially if it's a cheaper lockout.
Similarly, you would not try to change the position of a deadbolt on a door while the door is partially open.
Hydraulic lockouts can be activated on the fly.
Mechanical ones can't (in theory), because you the fork has to be completely extended. I can activate my mechanical lock on the fly by pulling up on the stem with one hand and turning the lock with the other hand, but it's a little dangerous:)
All forks can be unlocked on the fly.
You can just switch while riding along. Obviously, (assuming no bar mounted remote) you have to be going at a speed at which it's sensible to take one hands off the bars to open/close the fork. If you forget to unlock the fork and head down a descent you might want to stop/slow down a lot to switch it in that case.
Unless you have a bar-mounted lockout, there is of course a small risk involved in taking a hand from the bar, bending down and flicking the lockout, but other than that I can't see it being detrimental to the bike in any way.
All the lockouts I've tried require the weight to be off the fork though which usually necessitates a slight front-end lift to allow the lockout to be turned, so you probably have to account for that.
I certainly wouldn't do it in any kind of tricky terrain, or when travelling at speed, but I lock mine out or re-enable all the time on the move.