Use in when you want to express the idea "by virtue of" or "as a result of":
In submitting your application early you may improve your chances of being accepted into the program if the school has so-called "rolling admissions".
or
In calling him a liar publicly in print you have put your life in danger.
You have put your life in danger in calling him a liar publicly in print.
There is nothing temporal about that meaning; rather instrumentality is the underlying idea. You could change in there to by.
Use when when you want to express the idea of "whenever" or "while".
Wear goggles when working with caustic chemicals.
Wear goggles in working with caustic chemicals. ungrammatical
There are some activities where in with the ing-form of the verb casts the activity as a manner of doing, and in those cases in would be idiomatic:
Take care in how you mix these volatile chemicals.
Take care in mixing these volatile chemicals.
Take care in crossing the busy street.
There the meaning is not "whenever" or "while" but "in your manner of doing the thing" or "in how you do the thing". With in the focus is not on the occasion of the doing but on the doing per se, on the activity itself.