There are several things that could cause this, the CPU overheating, bad PSU, bad RAM. When you first start your computer look in the BIOS and see what your temperatures and voltage readings are and let us know what they are. You can also look in the event viewer to see what errors are occurring at those times, there will be a code which you can google for specifics.
Commonly, this can be ANY hardware problem ( hard drive, power supply, modem, case fan, cpu fan, floppy drive, CDR, CDRW, or DVD, sound card or NIC - any component getting eletrical power from the system ) as it is designed to shut down if trouble happens. It is also designed to shut down if it over heats, or if there is a major corruption of Windows.
The most frequent causes in HP machines, in our experience, are a failed hard drive, followed by a failed or failing cpu fan. Usually in the HP, the power supply quits altogether.
But don't over look the possibility of dust and pet hair. If the power supply and fan have been pumping air through the systemor a long time, you may have dirt packed up in the cpu fan housing or in the power supply.
Most HP cases are very difficult to get into unless you have experience. There are many case snaps that you can break off if you force them. But if you have skilled fingers and patience, you can get into the case and blow out all the dust with a can of difluoroethane gas such as dust off, or other canned air. DO NOT USE COMPRESSED AIR NOR A VACUUM CLEANER AS THEY CAN GENERATE STATIC ELECTRICITY THAT WILL RUIN THE COMPUTER.
Once you are sure it is clean, then you can begin testing components by unplugging all items you don't use. You can disconnect the hard drive and boot to a floppy if you have a Windows MSDOS, W95, W98, or WMe boot disk. If it runs without the hard drive, it is probably the hard drive.
We like to run the unit with nothing except the power supply, one memory module, video, and floppy boot. Let it run. If it does not shut down in that basic mode, you can think through the other stuff.
I actually have a similar problem. I have an Hp media center pc m7250n and it will close itself after a while. I'd be on the computer for about 15-20 minutes and then it will automatically shut itself off. Afterwards, I have to wait about a minute before I can restart it again. I thought this problem may have something to do with the fact that the dvd-rom broke and maybe something got jammed inside. I'd like to know what I can do to fix this problem
Sounds like an overheating problem to me.
There are lots of temperature monitoring software on the net. You should see your CPU temperature and if it gets over 60 °C on idle (or 70-80°C under load), then you know you have either failed CPU fan or extremely dirty heatsink.
If that is the case, keep in mind that CPU fans are cheap and much cheaper than a whole laptop. You should have it replaced.