After four days of negotiations capped by an all-night bargaining session, Hauser Communications of New York signed a contract yesterday to purchase Montgomery County's troubled cable television system for $40 million.

Officials from Hauser, which operates cable systems in Arlington County and suburban Minneapolis, and Tribune Co. of Chicago, which is selling its cable holdings, signed the contract at 6:17 a.m. and met with Montgomery County officials yesterday morning to inform them of the sale, said Hauser President John D. Evans.

Tribune Co. said it has invested $50 million in the Montgomery system, known as Tribune-United Cable Co. The sale is contingent on county approval.

County Executive Charles W. Gilchrist angrily announced a month ago that he was taking steps to revoke Tribune's franchise for repeated contract violations.

But despite yesterday's agreement, county residents could still see the system scaled down substantially from the 120-channel system promised by Tribune when it won the franchise three years ago. They also could face continued delays in receiving cable service until Tribune and the new buyers can resolve a host of issues that have plagued the system.

As a condition of the sale, Hauser has asked for substantial modifications in Tribune's contract to provide cable service, an issue over which the county and Tribune are fighting in court.

Company officials did not specify what modifications they are seeking. When Hauser acquired the Minneapolis system it sought changes such as cuts in public access programming and a reduction in the number of channels offered.

The Minneapolis system was facing problems similar to Montgomery's, according to Hauser officials here and in Minnesota.

Yesterday, Gilchrist released a short statement making clear that he would not release Tribune from its contract and approve the sale until all issues between the company and the county are resolved.

"We will continue our current efforts to enforce the existing franchise agreement with Tribune, and will not approve the sale until Tribune makes satisfactory arrangements to meet its obligations," Gilchrist said.

In addition to revocation proceedings, Gilchrist initiated efforts to seize $5 million in collateral and impose $9,000 a day in fines on the company for halting construction on the system and other alleged contract breaches. Tribune, however, won a temporary restraining order blocking the move last month in U.S. District Court.

The company, citing a year-old federal law, has maintained that it is entitled to relief from promises it made to build the system so that it could head off what it claimed were unforeseen and potentially huge financial losses.

With 228,000 households, Montgomery County would become Hauser's largest cable operation. There are about 20,000 subscribers in the Montgomery system, and about 75,000 homes could receive cable service now, according to county figures.

The system, however, is facing substantial problems. In addition to contract issues, the system is hampered by the fact that its major supplier of key electronic components has filed for bankruptcy, a move that has disrupted shipments of parts and forced a slowdown in cable subscriptions.

Earlier in the year, Tribune Co. said all 15 of its cable television properties were for sale, and last month it informed the county that it had received four bids on the Montgomery franchise, all below its $50 million investment in the system.

Before yesterday, Tribune had announced the sale of 13 of those systems.

Hauser president Evans said Tribune began intensive, nonstop negotiations on the Montgomery system four days ago in the Washington offices of Hogan & Hartson, an area law firm. The talks extended through Thursday night and into yesterday morning before an agreement was reached, he said.

"Our first priority is to get the transfer complete, and our second priority is to move forward with construction of what we believe will be one of the finest cable systems in the country," he said.

Hauser Communications was formed a little more than a year ago by Gustave Hauser, the former chief executive officer of Warner-Amex Cable Communications, the nation's sixth largest cable company.

According to industry analysts and other sources, Gustave Hauser is considered a pioneer of modern cable communications. He led Warner-Amex from 1973 to 1983, a period now known in the industry as the era of cable franchising frenzy.

Hauser won major franchises in Dallas, Pittsburgh, Houston, Cincinnati, Milwaukee and other cities by promising an elaborate array of services, including "QUBE," an advanced, two-way interactive system that allowed customers to shop and bank without leaving home.

He resigned from Warner-Amex under pressure in 1983, at a time when the company was facing huge losses on the construction and operation of its cable empire. His successor, former U.S. transportation secretary Drew Lewis, renegotiated many of the contracts Hauser had won.

The same year, Hauser reentered the cable market when his firm purchased a major share of the 34,000-subscriber Arlington County system, which had a history of financial troubles. The county was forced to renegotiate its contract with the company in 1981, said Evans, an early investor in the system.

Since Hauser took over the Arlington company, it has won two rate increases of slightly more than 7 percent each and has substantially reduced the number of customer complaints, said Gary Smyth, the county's cable administrator.