At the time of death, when one loses control of bodily processes, one's mind may jump from painful thought to painful thought. But this pain can be lessened or banished. In his book, Meditation and the Art of Dying, Swami Veda Bharati tells how to do so.
“When one has led a life with certain basic disciplines, whatever those disciplines might be—a life of unselfishness, a life in which one has fulfilled one’s karma, in which one has received and given unselfish love, a life that has not been built around contracts of one kind or another that are entered into at convenience and are broken licentiously, when in life there there have been permanent relationships, when in life there have been unselfish commitments: where one has dared to commit himself to people and principles over long periods of time, then the person has learned to gather the potency of the thought force. Even if he has not reached total liberation he will be still better off at the hour of death than someone who has not lived such a life."
"Those who have not lived such a life, their death is absolutely involuntary. Their time has come and the body is deteriorating; their thoughts are still way back into the past, six years ago, seven years ago; vengefulness, sorrow, grief, self-pity permeates, makes them cry; their body completely unable to stand on its own. This is uncontrolled, involuntary death. At the hour of death they have no direction."
"But even though you have not been a great master yogi and cannot avert the force of karma or the hour of death, but you have led a pure life you can at that time take a certain control over the death process. You can sit up and give direction to your thoughts. You can die with the name of God on your mental lips. Take, for example, someone like Gandhi. If someone came and shot me, what would be my last cry? Amidst a crowd of thousands of people the assassin came tearing through the crowd with his hands joined in salute but with a pistol hidden in his palms. He shoots. And the last words from Gandhi’s lips were, “Raam! Raam! This is the end of a life which was dedicated to absolute unselfish service and discipline. So there was no other cry of pain; his cry of pain was the name of God. This is within everybody’s reach.”
Dr. Usharbudh Arya (later, Swami Veda Bharati), Meditation and the Art of Dying, Pp. 80-81.