Do I stop wearing glasses when I have headache?

Maybe. If you feel your glasses are associated with causing the headaches then yes, remove them and get your refraction checked. If you already have a headache and your glasses are appropriate the it shouldn't make much difference whether you wear them or not. Sometimes though, heavy frames can exacerbate a headache.
Probably not. Glasses are supposed to make your eyes/vision comfortable and reduce the strain on them. If not wearing glasses help your headache, consult an optometrist.