You catch threadworm by ingesting an egg, or by having an egg
enter your body via the anus. Sounds a bit gross, but it happens.
The best way to prevent threadworm is to practice hygiene. It can
become a problem if feces isn't disposed of correctly in an area,
but developed countries don't generally have to worry about that
sort of issue.