Panda II thread handling?

I have been working off and on on an aquarium control via web server deal and have quite a bit of it sorted (web server thanks to jasdev) and am just wrapping stuff up as best i can when i came up to a question i have no good solution to.

I run separate threads for just about anything that i would except to happen at overlapping times, like measuring temperature, keeping timers, and ramping lights on or off. If i change some settings and need to restart those threads, like changing timers, its sometimes simple enough to check for a signal to kill and restart it in the thread itself. But for my ramping cycles i wanted to keep the code neat-ish (in my mind it is at least!) and condensed the 3 separate colors and up and down into a single method which i call into separate threads (overlapping cycles which can last hours). Now though, if i want to shut down one of the colors at any point how would i do that without affecting the others?

This is the method I have for all my ramp cycles.

        private static void Ramp(int start, int end, sbyte dir, int dur, PWM color)
            sbyte diff = (sbyte)System.Math.Abs(start - end);
            int durStep = (int)((dur * TimeSpan.TicksPerMinute) / (diff * TimeSpan.TicksPerMillisecond));
            for (byte x = (byte)start; start != end; start = (byte)(start + dir))
                x = (byte)(x + dir);
                color.Set(5000, x);

As i understand i have to kill the thread from within the thread, as i cannot control what a thread is called and as such trying to kill the right one would be shooting blindfolded… Is that wrong? But if i were to put a check in there for a bool they would all get killed when the bool came up. Also it doesn’t seem very elegant to start to add all these if checks (though thinking about it now i could probably change them all to while loops i guess) to everything i might want to stop at some point without restarting the device. It would have seemed much nicer to just call the timer thread the timer thread and killed that only to start it right back up. Even then all this may be silly and unnecessary i suppose if i used properties instead of fields (especially in the timer thread), but that is over my head still so i don’t know if that is remotely correct.

As an aside, is it possible to get the value of my x in that method (i realize its private and only exists inside it at the moment) for each cycle somehow? Now it know it would be simple to copy/paste the whole deal 2 times and call them x, y and z and it would be done, but I’d rather not butcher the code like that and leave it as is than know x if it cannot be solved in the single method solution. Knowing x would be a nice bonus is all.

Thanks in advance.

@ Gorgok, there are many ways to handle this. For .NETMF I would probably create a class called TimedRamp which managed the ramp stepping using a timer. Rather than having a loop, a timer would fire every ‘durStep’ milliseconds and trigger the next step. Stopping the ramp would be a simple matter of stopping the timer. The pseudo code for calling this might look something like the following

// Create an instance of the TimeRamp configured for the specific start, end, dir and duration 
var ramp1 = new TimedRamp(start, end, dir, duration,
  (step) => { color.Set(5000, step); });

ramp1.Start(); // Start the ramp
ramp1.Stop(); // Stop the ramp

If the ramp arguments are very dynamic, then you can shift the initialization arguments to the ‘Start’ method, that way you do not need to create a new instance each time the values change. You could have a mix as well, those decisions are dependent on how dynamic the system is with relation to these initialization arguments.

Hope this helps.