I have a method in a class that receives and returns multiple parameters from/to Form1. I need to use a timed event to execute some code using those parameters. I have arranged this simplified code to show the dynamic:
class Motor
{
public static System.Timers.Timer _timer;
int valReg = 30;
public void PID(decimal _actualSpeed, Decimal _speedRequest, out Decimal _pwmAuto, out decimal _preValReg)
{
_timer = new System.Timers.Timer();
_timer.Interval = (3000);
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timerAutoset);
_timer.Enabled = true;
// {....}
_pwmAuto = valReg;
_preValReg = valReg - 1;
}
static void _timerAutoset(object sender, System.Timers.ElapsedEventArgs e)
{
/* here I need to work with:
_actualSpeed
_speedRequest
_pwmAuto
_preValReg
and send back the last two variables
*/
}
}This is how I pass and receive the variables from Form1 button :
private void button4_Click(object sender, EventArgs e)
{
// some code ................
Motor mtr = new Motor();
mtr.PID(speedRequest, actualSpeed, out pwmAuto, out xxx);
//..more codeHow can I pass/get back those parameters to/from _timerAutoset event?
VulpesPosted Jun 4, 2013, 4:34 PM
However, if I'm understanding it correctly, the PID method will need to be suspended until the timer event has been stopped, otherwise it will return immediately before any processing has take place. An easy way to do this is to use a ManualResetEvent.
So, taking all this into account, the changes needed to the Motor class will be as follows: