A gunman invaded an immigration services center in downtown Binghamton, N.Y., during citizenship classes on Friday and shot 13 people to death and critically wounded 4 others before killing himself in a paroxysm of violence that turned a quiet civic setting into scenes of carnage and chaos.

The killing began around 10:30 a.m. and was over in minutes, witnesses said, but the ordeal lasted up to three hours for those trapped inside the American Civic Association as heavily armed police officers, sheriff’s deputies and state troopers threw up a cordon of firepower outside and waited in a silence of uncertainty.

Finally, officers who had not fired a shot closed in and found a sprawl of bodies in a classroom, 37 terrified survivors cowering in closets and a boiler room and, in an office, the dead gunman, identified as Jiverly Wong, 42, a Vietnamese immigrant who lived in nearby Johnson City.

Two pistols and a satchel of ammunition were found with the body. In what the police took to be evidence of preparation and premeditation, the assailant had driven a borrowed car up against the center’s back door to barricade it against escape, then had walked in the rain around to the front to begin the attack.