简体   繁体   English

系统使用的简单示例。 计时器。 C#中的定时器

[英]Simple example of the use of System. Timers. Timer in C#

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 .我阅读了 MSDN 站点和所有内容,但找不到关于如何引发接受参数的简单解释,该事件可以是stringdouble The examples provided uses ElapsedEventArgs but none shows a good way of implementing my own arguments to the raised events.提供的示例使用了ElapsedEventArgs但没有一个显示了对引发的事件实现我自己的参数的好方法。

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.我想要做的是Activate_Pressure是多余的,如果我可以将事件直接传递给我不知道如何传递的Pressure_Effect I am new to C# so please bear with me.我是 C# 新手,所以请耐心等待。 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:所以根据我们的评论线程,我看到了这样的事情发生:

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, ElapsedEventArgs 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.我认为将PressureEvent拆分得更多一点也不错。 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.在给定某些参数的情况下,我仍然会保留一个自己计算v的函数,然后一旦有了v您还需要做的任何其他事情也可以是它自己的方法。

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.您创建了 Timer 事件期望的签名的新方法,然后您省略了您不想要的参数并添加了一些您想要的固定值。 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.您可以使用匿名方法(更具体地说是 lambda)来执行您想要的操作。 This is the syntax for that:这是语法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM