Two police officers patrolling a housing project were shot and wounded by a gunman who apparently later killed himself with the same weapon a few miles from where Mayor Bill de Blasio was delivering his State of the City address on Thursday.

The officers were on the sixth floor of an apartment building in the Melrose Houses complex in the Bronx when they encountered two people in a stairwell, police said. One of the people pulled a gun and opened fire, and both officers were struck, one in the face and the other in the abdomen, police said.

The gunman fled into an apartment on the seventh floor, New York Police Department First Deputy Commissioner Ben Tucker said.

Officers responding to the scene found the gunman in the apartment dead of an apparently self-inflicted gunshot wound. A handgun and a shotgun were found inside the apartment, police said.

The second person who encountered the officers in the stairwell was in custody, and three people in the apartment were being questioned, Tucker said.

De Blasio, a Democrat, was told about the shooting as he finished his speech and left the stage. He met with the family of one of the officers at the hospital where they were being treated.

"Our brave officers were doing their jobs tonight in our public housing on patrol keeping residents safe," de Blasio said. "Both officers are alert and communicating, and we are praying for the best here."

The shooting happened about 5 miles from where de Blasio was delivering his speech, much of which was dedicated to praising the work of police officers.

One of the officers is a 29-year-old man, and the other is a 24-year-old woman, said Robert Boyce, the police department's chief of detectives. The officers, who are assigned to the Housing Bureau, have been on the force for about two years. Police would not provide the names of the officers or the suspect.

The head of the police officers' union, Patrick Lynch, said the shooting shows the dangerous nature of the job.

"We need your support to teach our young folks that pulling a gun on a police officer works for no one," he said. "This goes to show the dangers police officers face each and every day."

He said the shooting shows the difficulty and danger of vertical patrols, on which pairs of officers start in the lobby of a public housing project and walk the stairwells up to the roof and back down.

A police officer is on trial for manslaughter in Brooklyn after shooting an unarmed man during a similar patrol in November 2014. Rookie officer Peter Liang had his gun drawn in a pitch-black stairwell at the Louis Pink Houses in Brooklyn when he accidentally fired a shot. Akai Gurley was on a lower floor walking to the lobby and was struck and killed. Prosecutors say Liang was reckless and shouldn't have had his finger on the gun's trigger. Liang has pleaded not guilty, and his defense has suggested he had his gun drawn because of the dangerous nature of the assignment.

In January, a police officer responding to a large street fight in the Bronx was shot in an ankle. And in October, a police officer responding to a report of shots fired and a bicycle stolen at gunpoint in Manhattan's East Harlem neighborhood was killed.

___

This story has been corrected to show an officer was shot in the abdomen, not in an arm.