Is it Ok to lock or unlock my suspension fork while riding? 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. 

See Do I need to take my weight off the bike to lock out the suspension?