While you did stop the hammering by finding the source of initially oscilating valve, the true problem lies in poorly installed lines. Having worked under a great plumbers watchful eye, he always made sure we secured the lines throughout the walls. Water hammering is started by a valve opening and closing quickly, toilet, washer, dish washer, even shower valves can do this at times, but the real noise comes from the pipes banging on the studs and walls somewhere in the house. To really stop this from ever happening again you need to install a water hammer arrester. Sounds complex but it is simply a short piece of capped pipe (<1 foot) installed vertically somewhere in the system, being vertical it holds a small air pocket at the top that dampens & eliminates water hammering. You need to install one on both the hot and cold lines. With the new PEX piping this has become less of a problem as PEX seems to have a little give in it that dampens the hammering.
In all the houses and remodels I have done, I've always required my plumber to install water hammer arrestors like the ones described by Sliderulefarmer in the hot/cold lines adjacent to every electrically operated valve; washing machine, dish washer. Water hammer probably destroys more water mains and causes more damage inside houses than most other single events. I've seen 30 ton fire trucks actually moved sideways when a valve was incorrectly slammed shut on a 5" line. Good observation, connecting the toilet valve and knocking noise.
I had a similar problem recently. The fill valve (a Watermaster brand) was intermittently leaking, and for some reason (I didn't bother to autopsy the old valve, which I installed back in 1996, and which performed faithfully for nearly 15 years - no complaints) this would trigger the fill action; the float (or something internal) would stop the flow and then it would start again. Since this particular model of the valve shut off with a snap, there was a definite water hammer, at a frequency of about 2 cycles per second. This osillation could be started by a sudden pressure drop in the system (such as the washing machine fill valve opening) or it could self-start (usually about 3AM).
Needless to say, this was not likely to improve the plumbing system. I could generally stop it for a while (days) by jiggering the float valve arm, but a replacement valve solved the problem completely.
If there is a lesson from all this, it is that, in most houses, the toilet fill valve is the only automatic system in the plumbing. Everything else - washing machines, dishwashers, various faucets, etc. tends to operate only upon command. The toilet fill valve will operate whenever the water level in the tank drops or (in my case) from an internal malfunction. Way back when, I worked as a plumber for a few years, and the most common problem was toilet repair, so I learned to hate fixing toilets!
The repeated hammering is caused by the pressure wave reflecting back and forth in the line. The returning wave may cause the valve to open momentarily, and then it will close again and generate another wave. A method of reducing this is to usen a valve with a slow closure, and a method to damp the wave is to use an elasomeric connection hose. That provides the needed cusioning without adding anything extra to the system. The slower closing valve is equivalent to slew-rate limiting, used in electrical circuits to reduce interference.
Damage from water hammer is why piping should be well anchored, since there is often enough energy available to break the piping if it is unrestrained.
This valve is a good example of a closed loop servo system. The damping was set all wrong in the feedback loop. Unfortunately you probably can't adjust the loop. Fortunately it is a cheap replacement, but a pain in the neck...
A water hammer arrestor, field-fabricated from a short section of capped vertical pipe, is essentially useless. In some cases, these are installed by plumbers to meet code requirements. In other cases, they are installed by plumbing-ignorant people who think they are useful.
Why do I say they are useless? Because the air head diffuses into the water after a while, and the pipe becomes filled with water. This can happen in just a few weeks or less. It is possible to drain the water out of the arrestor (so it will work for up to a few weeks before again being drained), but homeowners generally never do this.
I once owned an older home that had a well and pressure tank equipped with a non-functional air volume control. The purpose of the air volume control is to maintain a head of air above the water in the pressure tank, so that the system pressure does not vary as much with water volume. This allows the pump to run longer and less frequently than it would with no air head in the tank. Most air volume controls were notoriously unreliable, so now most pressure tanks contain bladders that separate the water from the air. Every few weeks, I would have to turn off the power to the pump and drain a few buckets of water from the tank to create an air head.
Manufactured water hammer arrestors are available, which remain functional even if the homeowner does not drain them every few weeks. These are more costly than a short, capped section of pipe, so they are not used very often. In most household cases, water hammer is just a minor annoyance, not a serious problem.
We own a lake house in which we had a hammmer following every toilet flush. It sounded like something was busting through the wall. None of the plumbing is easily accesible so we tolerated it for the first month we owned the house and I figured sooner or later I would have to open a wall and put an arrestor in. One Sunday as we prepred to go home, the weather was getting cold enough that I closed the water main. The next time we came down and I turned the water back on, the hammer had disappeared and in the ensuing 4 years it has not returned.
I do not know what I did, but the silence is a pleasure
I worked as an engineer in the water heater industry for about eleven years and water hammer is a significant problem with some homes. The "hammer" can actually cause real issues and make repairs necessary. Ruptured piping and leakage are not uncommon. One "fix" was to install a pump tank; i.e. hydropneumatic vessel with a diaphragm that will "flex" and allow absorption of the pressures inside the piping system. It was always amazing to me how rapid closing of solenoid valves driving dishwashers and clothes washers could produce the water hammer. Equally amazing as to the pressures that could be developed.
Focus on Fundamentals consists of 45-minute on-line classes that cover a host of technologies. You learn without leaving the comfort of your desk. All classes are taught by subject-matter experts and all are archived. So if you can't attend live, attend at your convenience.