CHICAGO -- The dark days are finally in the past for Chicago White Sox left-hander John Danks.

With another strong performance, Danks appears to have regained his form following 2012 shoulder surgery, a 4-14 record in 2013 and early struggles this year.

In an 8-2 victory over the San Francisco Giants on Tuesday, Danks worked 6 1/3 innings for his third consecutive win, his best run since 2011.

"It's been a long, frustrating road to get back to here and there's certainly plenty to be worked on," Danks said. "There were some days when (I had) to fight, but I think I'm beyond that."

Danks (6-5) allowed two runs (one earned) and five hits while walking three and striking out four.

"He's really got it together from where he started early in the year," White Sox manager Robin Ventura said. "Since making some adjustments he's been spot on and he's really pitched well for us and really is the guy you can count on and we've been hoping for."

Second baseman Gordon Beckham and right fielder Dayan Viciedo each homered as the White Sox (34-37) snapped a four-game losing streak.

Beckham's two-run homer in the third inning erased a 2-0 deficit, while Viciedo's sixth of the season in the fifth extended Chicago's advantage to 8-2.

Giants right-hander Matt Cain (1-5) took his second defeat in a row. He gave up eight runs (seven earned) on 10 hits while striking out three and walking one over five innings.

The loss was the Giants' seventh in eight games and their fourth in a row. Despite the slump, San Francisco (43-28) still has the major leagues' best record.

Viciedo, Chicago's right fielder, finished 2-for-4 with two RBIs and two runs. Center fielder Adam Eaton and third baseman Conor Gillaspie added two hits apiece.

Right fielder Hunter Pence and third baseman Pablo Sandoval each had two hits for the Giants.

The Giants opened a 1-0 lead on a one-out, first-pitch home run by Pence -- his 10th homer of the year -- in the first.

They made it 2-0 when left fielder Gregor Blanco scored on designated hitter Buster Posey's single to left in the third. Blanco walked with one out and was safe at second when White Sox shortstop Alexei Ramirez bobbled a grounder.

Beckham tied the game at 2-2 in the third when he launched Cain's full-count pitch for a home run to left that also scored Eaton. It was the second baseman's fifth homer of the season.

"(Cain) got off to a good start; those first two innings he looked real sharp," Giants manager Bruce Bochy said. "It was looking like he was on track there. In the third inning he gave up a home run and then got derailed."

Giants catcher Hector Sanchez was struck on a swing by White Sox designated hitter Adam Dunn in the fourth but remained in the game. Chicago proceeded to load the bases with one out on the way to a three-run inning.

Left fielder Alejandro De Aza popped out in foul territory for a second out, but Cain walked catcher Tyler Flowers to bring home first baseman Jose Abreu for a 3-2 White Sox lead.

Eaton then beat out an infield single and reached second on an off-target throw by shortstop Brandon Crawford while Ramirez and Viciedo both crossed the plate to make it 5-2.

Chicago added three more runs in the fifth for an 8-2 lead. Dunn singled home Gillaspie with one out, and Viciedo homered to left with two outs, scoring Dunn.

"I felt confident in anything I was throwing," Cain said. "I made a really bad changeup pitch to Viciedo. The other ones weren't terrible, but they caught too much plate and the guys swinging the bat are going to take advantage of it."

NOTES: Fans and both teams paused for a moment of silence before the game in honor of Tony Gwynn, the San Diego Padres legend who died Monday at age 54. ... Giants OF Angel Pagan remained out of the lineup with back stiffness, and he is day-to-day. "He's making a little progress but not as much as we'd have hoped," manager Bruce Bochy said. ... San Francisco C Buster Posey was back in the starting lineup, serving as the designated hitter. He took a foul tip in the mask Sunday and was removed from the game against the Colorado Rockies as a precaution. ... The Giants send RHP Tim Hudson (7-2, 1.81 ERA) against White Sox LHP Chris Sale (5-1, 1.97 ERA) in Wednesday's series finale. ... Chicago CF Adam Eaton reached base in a 12th consecutive game. He finished 2-for-4.