The companies had been in talks for several months over whether Dish would continue to carry Viacom’s 18 channels, which include Nickelodeon, Comedy Central and MTV, and at what price, for its 14 million subscribers.

The deal provides some comfort for shareholders of Viacom which, like other media companies, faces an increasing threat to revenue as a growing number of consumers cancel cable subscriptions in favor of watching videos online.

Over the past few days, both companies went public about the potential of a blackout of Viacom’s programming if no agreement was reached by a midnight deadline.

The lack of programming interruption is notable. Dish tends to allow blackouts in 75 percent of its negotiations, according to a J.P. Morgan research report.

The companies declined to disclose financial terms.

The deal removed a weight on Viacom’s stock. Investors and analysts had worried that Dish, whose Chief Executive Charlie Ergen is known as a tough negotiator, would drop Viacom programming, causing other distributors to follow suit.

“It feels like enough of a win even without knowing the terms of the deal,” said Sal Muoio, whose investment firm is a major owner of voting Viacom shares. “With this out of the way, Viacom’s share price will have the opportunity to reflect the fundamentals of the business without the overhang of this.”

Viacom’s stock was up 11.3 percent at $41.62, and Dish rose 2 percent to $48.38, both on Nasdaq.

The deal is good news for Viacom CEO Philippe Dauman, who was named executive chairman in February. The media company has been struggling to turn around its poor television ratings, and the agreement with Dish shows that distributors still view Viacom’s networks as valuable to their viewers.

Failure to reach a deal would have been a red flag for other media companies, analyst Brian Wieser of Pivotal Research said.

“If Dish had cut Viacom and only saw modest subscriber losses it would have emboldened other distributors with other networks,” he said. “No one is paying attention to Cable One or SuddenLink Communications [CQUELS.UL], but they do pay attention to Charlie Ergen.”

Cable One and Sudden Link, which are smaller cable operators, dropped Viacom programming last year.

In the past year Viacom had penned distribution agreements with AT&T, Charter Communications and other cable companies, whose subscribers total more than 44 million, Viacom said.

“In 2017 and beyond, these deals will drive Viacom’s annual affiliate fee growth with our traditional distribution partners at solid mid-single-digit rates,” the company wrote in an e-mailed statement.

This would mark an improvement from 2016, for which Viacom has estimated affiliate growth in the low-to-mid single digits.

Under the new contract, Viacom’s programs will appear on Dish’s $20-per-month Sling TV online streaming service that offers a slim bundle of channels.

Viacom has other challenges weighing on its stock, said Wieser, citing a lawsuit over the mental competence of its controlling shareholder Sumner Redstone. The trial is scheduled in May.

Additional reporting by Jessica Toonkel in New York and Rishika Sadam in Bengaluru; Editing by W Simon and Richard Chang