Hi,
we have a system variable called sy-batch (used for Program is running in the background ).
if program is run in background it will "X" if in foreground then it will space.
note
1.put all code in appropriate Perform for code modularisation.
2.in program use system variable
if sy-batch = 'X'.
<Background activity>
else
<Fore Ground activity>
endif.