Here is some advice from Saint Theophan the Recluse:
First we are to pray as follows:
Pray with sincere warmth, with an outpouring before God, with feelings of contrition, humility and reverential fear and with diligent petitions for your spiritual needs.When we even approach prayer in church with this attitude we will still be faced with stray thoughts. Here is how how recommends to deal with them
As soon as you notice your thoughts have left church, turn them back and do not ever allow yourself consciously to daydream or to stray in thought... When the thoughts stray unbeknownst to you, this is also a small sin; but when you begin wandering off in thought purposely while you are in church , this is as sin...It is like a man who has gone to the king in order to ask him for something who then begins to make faces and fidget in the king's presence, without paying any attention to him... With respect to thoughts there are these two rules: 1) As soon as you notice this straying, turn the thoughts back, and 2) do not consciously allow the thoughts to wander.
A remedy against straying thoughts is mental attention, attention to the fact that the Lord is before us and we are before Him.... The attention is attached to the Lord by fear of God and by the desire to please God. From these come warmth of the heart, which draws the attention to the One Lord... Without labor and mental effort you will not attain anything spiritual...
Reference: The Spiritual Life, pp 151-152