For the first time, a study shows that a drug used to treat HIV infection also can help prevent it when taken before and after risky sex by gay men. The results offer hope of a more appealing way to help prevent the disease beyond taking daily pills and using condoms, although those methods are still considered best. The study, done in France and Canada, is the first to test “on demand” use of Truvada, a pill combining two AIDS drugs, by people planning to have risky sex. The uninfected men who took it were 86 percent less likely to get HIV compared to men given dummy pills. “That impressed me,” Dr. Scott Hammer said of the size of the benefit. He is an AIDS specialist at Columbia University in New York and heads the Retrovirus Conference going on in Seattle, where the results were discussed Tuesday.