I read the MSDN site and everything but I cannot find the easy explanation on how to raise a timed event which accepts arguments which can be a string
or double
. The examples provided uses ElapsedEventArgs
but none shows a good way of implementing my own arguments to the raised events.
My code (I have not tested so might be wrong):
private double Pressure_Effect(double p, int t){
time=(double) t;
v=(((p/(rho*y))-g)*time)/(1.0+(mu*time/(rho*y*x)));
return v;
}
private void Time_Handle(){
System.Timers.Timer startTimer=new System.Timers.Timer(70);
startTimer.Elapsed += new ElapsedEventHandler(Activate_Pressure);
}
private void Activate_Pressure(object source, ElapsedEventArgs e){
pressure=2.5;
double v=Pressure_Effect(pressure, 70);
}
What I want to do is Activate_Pressure
is redundant the sense that if I can pass the event directly to Pressure_Effect
which I don't know how. I am new to C# so please bear with me. I know I have not enabled the timer and other critical parts might be missing in this code but I just posted it to make my question clear.
So based on our comment thread, I'm seeing something like this happening:
I don't think it's bad to split up
PressureEvent
a bit more. I would still keep a function that calculatesv
on its own, given certain parameters, and then whatever else you need to do once you havev
could be its own method as well.So you want to ignore some of the arguments passed to the given event handler, and add some additional ones of a fixed value. You have just shown how you can do that; you make a new method of the signature that the Timer's event expects, and then you omits the arguments you don't want and add in some fixed values that you do want. You are correct to think that this is a rather verbose method of accomplishing what you want. There is indeed a more terse syntax. You can use an anonymous method, more specifically a lambda, to do what you want. This is the syntax for that: