Over the weekend, police say, Rui Jiang posted a photo on Instagram of a gun being pointed at a computer screen showing the spire of a white church building.
The words "Beautiful Old Churches Across America" were superimposed over part of the image.
A series of such posts featured vague threats of violence, police say. One showed the lighted sign above the rain-slicked entrance to Park Valley Church in northern Virginia's Prince William County. The burning pages of a Bible were seen in another.
On Sunday morning, Jiang, 35, was arrested at Park Valley Church in possession of a loaded handgun with an additional magazine, the Prince William County Police Department said. He was dressed in black and wearing sunglasses.
Jiang, a Virginia resident, was picked up after a woman in Anne Arundel County, Maryland -- about an hour east of the church in Haymarket -- notified local police of the threatening online posts. The woman told police she knew Jiang.
"It was an imminent disaster, you know, that could have happened here, that didn't," Pastor Barry White told CNN at the church on Tuesday.
Jiang was arrested during Park Valley's 10 a.m. service -- the church's most popular, with about 1,000 people, including 300 children, in attendance, according to White. The children have their own ministry in another part of the sprawling church.
Fairfax County Police Chief Kevin Davis said he believed the arrest halted "America's latest version of a mass killer."
"We put our hands on him, literally, in the nick of time," the chief said.
Jiang's arrest comes at a time when places of worship across the country have been repeated targets in America's scourge of mass shootings, including a 2018 shooting at the Tree of Life Synagogue in Pittsburgh and a 2017 shooting at the First Baptist Church in Sutherland Springs, Texas.
"It's sobering to think about it," White said.
Jiang managed to enter the church through a rear door, White said. He was spotted by security personnel and followed into the vast sanctuary, where he sat in the stadium-style seating on the top level for a moment before getting up and going through a door marked, "Do not enter."
Security personnel confronted Jiang, who returned to the sanctuary, descended the stairs and walked out into the lobby, according to White. Again, he was confronted by security personnel as Prince William County officers arrived.
A county police officer took Jiang outside and discovered that he was armed, White said.
He also was in possession of a folding knife and a folding "credit card" style knife, the police statement said.
Jiang worshiped at the church over the summer, the pastor said.
"They're still trying to figure out the connection as to why, you know, he would choose our church," said White, who added that Jiang wrote on social media that he believed government employees attended the church.
Jiang faces a felony charge of making a threat by letter to intimidate a population and a misdemeanor charge of carrying a dangerous weapon to a place of worship, according to Daniela Salguero from Prince William County's Office of the Commonwealth's Attorney.
He was arraigned Monday and assigned a public defender. The Prince William County public defender's office declined to identify his attorney to CNN. Jiang's next court hearing is set for October 11, online court records show.
"As bad as it was, it was a victory," said White. "Somebody saw something, said something. Then you had all these police organizations that worked flawlessly together."
'He arrived at the church with a mission'
After being tipped off about the social media posts, police in Anne Arundel County contacted investigators in Fairfax County, where Jiang lived, and officers were sent to his home.
Jiang wasn't home, but investigators were also able to give a description of his vehicle to officers in Prince William County.
An off-duty officer working an assignment at the church spotted the vehicle in the parking lot.
"At around the same time, staff at the church were monitoring a suspicious person who was determined to be the accused who was on the premises. The off-duty officer and church staff coordinated and detained the accused near the entrance without incident," the statement from Prince William County Police said.
The officers who searched Jiang's home seized documents and electronic items, but no additional firearms were located, according to the statement. Jiang "was determined to have a concealed weapon permit, and the firearm was not reported stolen," the police statement reads.
Letters and other documents related to the case were also found in the home, Prince William County Police Sgt. Jonathan Perok told CNN.
Davis, the Fairfax County police chief, said Jiang had staked out the church in the predawn on Sunday.
"He was armed. He arrived at the church with a mission," Davis said. "We captured him before he was able to take many, many lives, in my opinion."