You can place your code which only compiles in .NET 4
in seperate dll, load the relevant classes, methods etc..
by reflection.
So let's say you have a method which "sometimes" need to start a new task=
You should put the task hadeling in seperate dll,
if the version is suitable, you can than load the class from the dll and run its method by reflection.
you should compile your code in .net2 only and the external dll in .net4 only.
In runtime you decide if you load the .net4 dll or not based on a configuration value,
or if you want by checking if the computer has .net i installed on it:
Is there an easy way to check the .NET Framework version?