Byron Farwell, 78, a writer, historian and civic activist who had served six years as mayor of the town of Hillsboro in the Blue Ridge foothills of western Loudoun County, died Aug. 3 at Loudoun County Hospital after a heart attack.

Mr. Farwell was the mayor of Hillsboro from 1976 to 1982, losing his bid for a fourth two-year term when his opponent's name was drawn from a punch bowl at the Loudoun County Courthouse after the election ended in a 17-17 tie. As the mayor of Hillsboro, which with a population of 125 had long claimed to be Virginia's smallest town, he fought a running battle with state and federal officials on issues ranging from the census count to health department regulations.

As a historian and writer, Mr. Farwell was the author of 13 books of military history and biography. He specialized in British military history. His works included "Queen Victoria's Little Wars," "Mr. Kipling's Army," and "The Man Who Presumed," a biography of Henry M. Stanley.

He had also written articles, opinion pieces and book reviews for The Washington Post, the Washington Times, Military History magazine, American Heritage, Harpers, Smithsonian and others. These ranged the gamut from pieces defending the pastoral lifestyle of western Loudoun County against encroaching development to an attack on a Loudoun County requirement that poets and writers obtain a business license and post it wherever they practiced their craft. "God only knows what life must be like in counties where the government allows writers to run free," he wrote in a 1987 article in The Post.

A native of Manchester, Iowa, Mr. Farwell served in the Army during World War II and participated in the invasion of North Africa. He also served in the Korean War. He attended Ohio State University and received a master's degree in English at the University of Chicago.

From 1954 until 1971 he worked for Chrysler Corp., and for much of that period he was director of administration for Chrysler International in Geneva, Switzerland. In 1971 he left Chrysler and settled in Hillsboro, which overlooks North Fork Catoctin Creek, midway between Leesburg and Harper's Ferry.

"This is, after all, not merely another dormitory community for Greater Washington," Mr. Farwell wrote in a 1986 Post article arguing against increasing residential and commercial development in the region. "It is a remarkable land of low mountains and steep hills, part of the northern piedmont area of the Blue Ridge, still forested and teeming with wild creatures. Its rich valleys give bumper crops and its pastures are filled with horses and cows. Its small towns are ornamented by old stone houses, refurbished log cabins, 19th century brick homes and white clapboard Victorian houses, wrapped by wide porches. From our side door we can step onto an abandoned road down which Lee's cavalry is said to have retreated after Antietam. A two-minute walk and we are in a forest. . . .

"What can be wrong in not wanting to pay higher taxes, not wanting to see pleasant pastures and woodlands turned into Levittowns, not wanting to creep on crowded highways, not wanting to queue up at banks, stores and gas stations, not wanting to search for parking places?"

In 1976 Mr. Farwell began his first term as mayor of Hillsboro, a position that paid $50 a year. He presided over a municipality that charged $15 a year for auto tags, $2 a month for water and $1 a week for trash collections, and it had always collected more money than it spent. But he soon found himself at odds with the Virginia Health Department.

"Hillsboro's budget would not be so healthy if its elected official had obeyed all the orders it received from bureaucrats," he wrote in 1980. "The town's expenses would have doubled, for example, had they obeyed all the silly edicts of Virginia's Health Department, which had nothing to do with health.

"Hillsboro's spring has always provided the town with enough water, but `enough` is not a statistic that a bureaucrat can deal with, so the town was ordered to install a meter to measure the flow of water. The town refused.

"The Health Department demanded that the town publish newspaper advertisements, often within incomprehensible copy provided by a functionally illiterate bureaucrat, telling our 26 water customers whenever something went awry with our water system. Since the government communicates with its citizens by way of the bulletin board at the post office, there seemed to be no reason to buy advertisements. None was purchased."

When the 1980 census came around, Mr. Farwell clashed once again with the bureaucracy, this time from the federal government. The initial count was wrong, he was sure, so he asked for a recount, but that was wrong too. Before it ended, there had been five separate census tallies that produced reports of 34, 39, 41, 37 or 38 dwellings, housing a population of 98 or 100 or 104 or 109 or 135 in Hillsboro. Eventually, Mr. Farwell said, he was told by a Census Bureau spokesman that "the resources of the bureau have been exhausted."

In a 1981 article in The Post, he wrote, "I pictured tens of thousands of supine census bureaucrats, exhausted by their efforts to count the 41 houses of Hillsboro."

Running for re-election in May 1982, Mr. Farwell lost his office to Alexander F. Muir, a goat farmer and retired engineer, when Muir's name was pulled from a punch bowl after the 17-17 tie. Thirty-five of Hillsboro's 67 registered voters had cast ballots, but one expressed no preference. Some voters said they wanted a mayor who would pay more attention to them and less to his writing.

Mr. Farwell was a trustee of the Oatlands of the National Trust plantation outside Leesburg and a fellow of the Royal Society of Literature and the Royal Geographic Society. From 1978 to 1980 he was the Wallerstein Fellow of the Institute of Government at the University of Virginia. He had traveled extensively around the world.

Survivors include his wife of 58 years, Ruth Farwell of Hillsboro; three children, Joyce Lewis of Coal Harbour, British Columbia, Byron J. Farwell of Gilbert, Ariz., and Lesley Pesnicak of Stafford, Va.; and three grandchildren.