Mark Wahlberg, one of the world’s most successful high school dropouts, stopped by T.C. Williams High School in Alexandria on Wednesday morning to encourage students to stay in school.

The multimillionaire Hollywood producer, actor, former rapper and model dropped out of high school in the ninth grade. But now, the 41-year-old father of four is pursuing his diploma through an online credit-recovery program.

“If my career goes south, I’m working at McDonald’s. I’m driving a tow truck,” he told the students. “That’s why I’m going back to high school.”

T.C. Williams has been working aggressively to improve its graduation rate since 2010, when it was labeled one of the nation’s persistently lowest-achieving schools and given federal funding to improve.

In 2009, 78 percent of students graduated from T.C. Williams in four years, compared with 83 percent across Virginia. Just 65 percent of Hispanic students graduated in the same time. By the 2012-13 school year, the overall on-time graduation rate increased to 82 percent — and 70 percent for Hispanic students.

School officials said the improved graduation rates are the result of a much larger team of school counselors working closely with students to develop personal academic and career goals and helping them follow through.

“They know these children inside and out, their families, their social histories and academic performance,” said Gregory Forbes, director of secondary school counseling. “Truly, no student is slipping through the cracks.”

Alexandria, like a growing number of school districts, is also beginning to offer more online courses to help students at risk of dropping out pursue their degree on a faster or more flexible timeline. The city’s high school opened a satellite campus in the fall at Landmark Mall, where students can follow a computer-based curriculum at their own pace.

Officials say the flexibility is crucial for students who have to work, take care of children or have other complications in their personal lives.

Wahlberg set out to pursue his own degree through a similar program. He enrolled in an accelerated online program at Snowden International High School, formerly Copley Square High School, where he would have graduated in 1988, according to Gina Alfonseca, a spokeswoman for Boston Public Schools.

Snowden’s headmaster invited Wahlberg to finish his diploma there after reading in a celebrity magazine that he regretted dropping out, Alfonseca said.

Since then, Wahlberg has become a spokesman for staying in school. He visited T.C. Williams after the school won a national contest.

A DJ transformed the early morning assembly into a nightclub, with blasting music and 1,100 students’ hands and cellphones swaying in the air. Wahlberg greeted a shrieking audience. He flirted with Principal Suzanne Maxey and, with cameras rolling, told the students about his rocky adolescence in a rough Boston neighborhood.

He was one of nine children. He ran around with a street gang and clashed with the law.

“We thought we were having fun drinking and getting high,” Wahlberg said. But the story did not end well for everyone.

“Most of my friends are incarcerated,” he said. “This is a much nicer place to be.”

After he served time for an assault charge at 16, he decided to change his life. He started going to church and found a passion for music and, later, acting, he told the students.

Wahlberg was scheduled to visit the White House with a student and college counselor later Wednesday to speak with an adviser about the importance of improving graduation rates, an Alexandria schools spokesman said.

Hendrik Enriquez, 18, said Wahlberg’s message was helpful. “High school gets tired sometimes,” he said.

Three of Enriquez’s friends dropped out of high school, but he said he plans to go to college.

“Almost there,” he said.