We already have discussed the Hadith of `Ammar ibn Yasir (may Allah be pleased with him) which contains the great invocation that the Prophet (peace be upon him) used to say during his Salah.
The Hadith was reported by An-Nasa'y and others on the authority of `Ata' ibn As-Sa'ib from his father (may Allah be pleased with him) who said: "`Ammar ibn Yasir led us in Salah and he made it brief. Some of the people said to him: 'You made the Salah short (or brief).' He said: 'Nevertheless, I still recited supplications that I heard from the Messenger of Allah (peace be upon him).' When he got up and left, a man -he was my father but he did not name himself- followed him and asked him about that supplication, then he came and told the people. "Allahumma bi `Ilmikal-Ghaiba wa Qudratika `Alal-Khalqi Ahyiny Ma `Alimtal-Hayata Khairan li, Wa Tawaffany Idha `Alimtal-Wafata Khayran li. Allahumma As'aluka Khashyataka Fil-Ghaybi wash-shahadah, wa As'aluka Kalimatul-Haqqi fir-Rida wal Ghadab. Wa As'alukal-Qasda fil Faqr wal-Ghina, wa As'aluka Na`iman la Yanfadu, wa As'aluka Qurrata `Aynin la Tanqati`u wa As'alukar-Rida Ba`dal-Qada' wa As'aluka Bardal `Ayshi Ba`dal-Mawt, wa As'aluka Ladh-dhatan-Nazari ila wajhika Wash-shawqa ila Liqa'ika fi Ghayri Darra' Mudirrah wala Fitnatin Mudillah. Allahumma Zayyina Bizinatil-Iman, waj`alna Hudatan Muhtadin (O Allah, by Your Knowledge of the unseen and Your Power over creation, keep me alive so long as You know that living is good for me and cause me to die when You know that death is better for me. O Allah, cause me to fear You in secret and in public. I ask You to make me true in speech in times of pleasure and of anger. I ask You to make me moderate in times of wealth and poverty. And I ask You for everlasting delight and joy that will never cease. I ask You to make me pleased with that which You have decreed and for an easy life after death. I ask You for the sweetness of looking at Your Face and a longing to meet You in a manner that does not entail a calamity that will bring about harm or a trial that will cause deviation. O Allah, beautify us with the adornment of faith and make us among those who guide and are rightly guided." [Has been previously attributed to its sources].
This Hadith has a great benefit and contains great meanings and useful indications related to `Aqidah (creed), acts of worship, and morals. The benefit which a Muslim gets from such blessed invocations is that he knows its meanings and understands its indications and objectives, and fights his desires to achieve them. In the following is a pause with some of these meanings (for more benefit, see the book of the explanation of the Hadith of `Ammar ibn Yasir (may Allah be pleased with him) by Ibn Rajab).
As for the Prophet's saying: "Allahumma bi `Ilmikal-Ghaiba wa Qudratika `Alal-Khalqi Ahyiny Ma `Alimtal-Hayata Khairan li, Wa Tawaffany Idha `Alimtal-Wafata Khayran li," it refers the whole matter to Allah and asking Him for goodness in all person's affairs and asking Him with His Knowledge which encompasses everything.
It also contains recognition that Allah knows the hidden secrets of things as He knows their apparent conditions. By Allah's Power over all creatures, there is none to put back His Judgment and none can ward off His destiny. It is known that a person does not know the consequences of things while a person is unable to achieve goodness or push away harms except by the means which Allah created for him. Hence, a person is in dire need to the All-Knowing, the Omnipotent to fix his entire conditions and choose goodness for him, therefore the Prophet said: "Ahyiny Ma `Alimtal-Hayata Khairan li, Wa Tawaffany Idha `Alimtal-Wafata Khayran li."
Consequently, the prohibition was mentioned in the Sunnah about wishing death due to a harm that hits a person because a person does not the consequences. It was reported in Sahih Al Bukhari that the Messenger of Allah (peace be upon him) said: "None of you should long for death, for if he is a good man, he may increase his good deeds, and if he is an evil-doer, he may stop the evil deeds and repent." [i.e., seeking the pleasure of Allah by giving up sins and asking for forgiveness].
As for the Prophet's saying: "Allahumma As'aluka Khashyataka Fil-Ghaybi wash-shahadah," i.e., I fear You Allah in the public and in the secret and in case I am in public [among people] or in private because some people fear Allah in public, but the most important thing is to fear Allah in secret when a person is alone and away from the people. Allah praised those who fear Him. He says: "Verily, those who fear their Lord unseen (i.e. they do not see Him, nor His Punishment in the Hereafter), theirs will be forgiveness and a great reward (i.e. Paradise)." [Surat Al Anbiya': 49]. Allah (Glory be to Him) says: "Who feared the Most Gracious (Allâh) in the Ghaib (Unseen) and came with a heart turned in repentance (to Him and absolutely free from each and every kind of polytheism)." [Surat Qaf: 33].