Helping you with phimosis, frenulum breve, tight foreskin, and other male health issues.
The reality is that most guys don't have a choice in the matter.
Most guys will not have an easy time staying erect while stretching their foreskin or frenulum. Stretching is not a sexually stimulating activity. If anything, stretching the foreskin creates discomfort since tension is being applied to the phimotic ring. This sort of pain typically subsides any sort of erection, and involuntary erects generally don't last long enough for a full stretch session.
However, if you do somehow have a choice between stretching when erect or flaccid, it's easier to stretch the foreskin when your penis is erect.
I will note that it's harder to retrieve your foreskin if it gets stuck behind the erect glans, so beware.
Otherwise, an erect penis would be a useful stretching device if the erection stayed. For example, you would be able to perform a good stretch by gently pulling back your foreskin as far as it will go and the erect glans will create tension.
This kind of stretch usually doesn't work when flaccid since the flaccid glans is so spongey.