Simple example of the use of System. Timers. Timer

2019-04-17 17:33发布

问题:

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.

回答1:

So based on our comment thread, I'm seeing something like this happening:

class PressureModel
{
    private double interval = 70;
    private double pressure = 2.5;
    private DateTime startTime;
    private Timer timer;

    // assuming rho, y, g, x, and mu are defined somewhere in here?

    public PressureModel()
    {
        timer = new Timer(interval);
        timer.Elapsed += (sender, args) => PressureEvent(sender, args);
    }

    public void TimerStart() 
    {
        startTime = DateTime.Now;
        timer.Start();
    }

    public void TimerStop()
    {
        timer.Stop();
    }

    private void PressureEvent(object sender, EventElapsedArgs args)
    {
        // calculate total elapsed time in ms
        double time = (double)((args.SignalTime - startTime).TotalMilliseconds);
        // calculate v
        double v = CalculateV(time);
        //
        ... do you other work here ...
    }

    private double CalculateV(double time)
    {
        double p = this.pressure;
        // not sure where the other variables come from...
        return (((p/(rho*y))-g)*time)/(1.0+(mu*time/(rho*y*x)));
    }
}

I don't think it's bad to split up PressureEvent a bit more. I would still keep a function that calculates v on its own, given certain parameters, and then whatever else you need to do once you have v could be its own method as well.



回答2:

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:

startTimer.Elapsed += (s, args) => Pressure_Effect(2.5, startTimer.Interval);


标签: c# timer