If you are talking about a specific "period of time", you use -ken (when doing, while doing)
for example;
Okula giderken onu gördüm.
When I was going to the school, I saw him.
BUT
Okula gittiğim zaman / Okula gittiğimde onu gördüm.
When I went to the school I saw him. (not on the way, at the school)
another example;
Okula giderken ödevimi yaptım. I did my homework while going to the school.
Okula gittiğim zaman ödevimi yaptım. I did my homework when I went to the school. (not on the way but at the school)
You should use -ken only when you think it will not change the meaning; like when baking, when watching. Because; if you look at the meaning it makes no difference to say kek yaparken or kek yaptığım zaman as you can not bake the cake in a second. It is easier to use -ken as it is same for all subjects and (hard to believe but) never changes in any tense. But be aware if you also tell what is happening at the same time period.