I don't really know a solution that puts an absolute end to it, I have difficulties with this too. I hope we hear more about this from people with more experience. This is what I have learned so far.
In fact one saint, St. John the Dwarf, pleaded with God to take away the mental attacks of the devils, and His prayer was answered with a yes. For a short while he wandered around the desert, looking rather lost I imagine, and then he told one of His brothers, that he had nothing to do with his time, so he asked God to give him back his trials, and then he went on, fighting the trials, but more satisfied.
In other words, this sort of mental battle may be something we will undergo to a certain degree at least, until the end of our days.
The way we think, the way our memories work is by habit and reinforcement. So if a demon sends us a blasphemous thought, and it makes an impression, it gets stuck in our mind via memory, and so inadvertently, then it comes to us again, and so the confusion..
And often we are thinking about prayer or some subject, and then the association between one thing and another happens, and the demons do their best to reinforce this association, and cause all sorts of problems.
The demons work especially with the imagination.
I think if we become especially focused on God, and learn to pray well, these attacks will largely only occur on the exterior part of our soul and not trouble us within -- on the other hand, some saints appear to have been particularly tried and attacked in ways interiorly.
And also we should convert our hearts so our hearts reject and hate all sin, and love God so fervently, so that I think when these things come to us we know they are so against our will that we can feel at peace that we have no guilty collusion with them.
We should feel comforted that the saints go through these trials too at the least.
So we take a deep breath.. and try to find our peace in prayer.. despite these things and not to let them disturb them, not to let them be our focus, but put them aside.. if necessary with anger.. if possible just by thinking of something else firmly or trying to build new good mental associations.. and focus on Christ and His peace, and contemplation of Him and His passion.
These attacks are meant to stop us from being devoted and praying. So above all else we must not allow them to so dishearten us as to cause us to neglect our devotion to God, but rather see them as a sign that we should increase our devotion, because obviously we are achieving some good success by it and so arousing opposition that would otherwise not appear.
Here are some words that may help.
'The enemy kept to this particular method by giving her for a long time the temptation of blasphemy – for which she never found any remedy either in confession or in any other way – until finally the devil came to her one night while she was sleeping and opened her ears and told her to blaspheme God. She, still sleeping, refused, saying: "I will not do that." And when the evil one saw how much she despised him, he made such a loud racket that she woke up and felt him depart. And in this way, she realized clearly that it was the enemy who had afflicted her so by putting in her heart such blasphemies and then leading her to think that they came from herself in order to make her fall into despair. And after this, she remained victorious over temptation, seeing openly how the enemy insinuated such blasphemies into her spirit. So if any of you, beloved sisters, should be tempted in a similar struggle, do not be alarmed nor saddened by thinking that it proceeds from yourself rather than only from diabolic envy which cannot stand it that God be adored and praised. But in eternity, without any respite, he will be blessed and praised and magnified and superexalted in despite and derision of Lucifer and all his companions and the dark brigade. Amen. Amen.'
St. Catherine of Bologna, 'The Seven Spiritual Weapons'
'The incensive power usually troubles and confuses the soul more than any other passion, yet there are times when it greatly benefits the soul. For when with inward calm we direct it against blasphemers or other sinners in order to induce them to mend their ways or at least feel some shame, we make our soul more gentle. In this way we put ourselves completely in harmony with the purposes of God's justice and goodness. In addition, through becoming deeply angered by sin we often overcome weaknesses in our soul. Thus there is no doubt that if, when deeply depressed, we become indignant in spirit against the demon of corruption, this gives us the strength to despise even the presumptuousness of death. In order to make this clear, the Lord twice became indignant against death and troubled in spirit (cf. John 12:27, 13:21); and despite the fact that, untroubled, He could by a simple act of will do all that He wished, none the less when He restored Lazarus' soul to his body He was indignant and troubled in spirit (cf. John 11:33) - which seems to me to show that a controlled incensive power is a weapon implanted in our nature by God when He creates us.'
St. Diadochos of Photiki
'Humble yourself before God; lose yourself in the very depths of that sacred desert of which I have, spoken to you; be absorbed by the Sovereign Good, consumed by the sacred fire of love. Believe me, this divine fire will dissipate all the dust and mist of scruples, and your soul will become purer and more beautiful in the eyes of your heavenly Spouse.'
'Do not let the devil frighten you. Keep yourself hidden in God, and nothing can hurt you.'
St. Paul of the Cross