For follow-up discussion and general commentary on the topic. Comments are sorted chronologically.
A few years ago a thought popped into my head several times that it was wrong to have sex with my partner because we weren't married. Shamefully I carried on doing it though. I wish I hadn't. My partner died in July and I quit my job and just wallowed in self pity. I'm ever conscious that by doing this I'm Mocking God and committing blasphemy. I'm starting a new job Wednesday next though. On top of that I've been having all kinds of religious OCD, really nasty stuff. I haven't repented yet, I want to though because I'm sick of living in sin. Is it to late for me though?
I think you've repented and don't realize it. All I'm reading here is repentance.
When did you repent? Read again your first 4 words. "A few years ago."
You didn't stop having sex with your partner, but you had a change of heart about what you were doing. You admitted to yourself that it was not right.
According to you, the idea that you shouldn't be living the way you were just 'popped into your head.' Is that how it's done? Did any other sin that you habitually commit get singled out for you to analyze, or was having sex with your partner the only one?
I hope you get my meaning.
I'm speaking from experience here. When God causes a sin you're habitually committing to "pop into your head" it's usually just the beginning. If you gossip, that sin starts to bother you too. If you prevaricate (lie) it starts to make you feel funny inside. If you lose your temper and speak unseemly, it will bother you.
It's because God HAS BROUGHT YOU TO REPENTANCE!!
It's easy to miss the fact that gossiping, lying and losing your temper have begun to stand out when you do them, because your attention is on the major sin in your life. You're predisposed to concentrating on your "LIVING IN SIN" lifestyle, as if it's the only fault you have.
You say you kept doing it. I'm going to guess that you continued IN ALL YOUR OTHER WRONGDOINGS!!
Repentance is God changing your mind (heart) about sin. How else would it "pop into your head?" You're fine. Pray for him to draw you closer; he will.