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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18337130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:58:14  来源:igfitidea点击:

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

c#timer

提问by Hyman_of_All_Trades

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 stringor double. The examples provided uses ElapsedEventArgsbut none shows a good way of implementing my own arguments to the raised events.

我阅读了 MSDN 站点和所有内容,但找不到关于如何引发接受参数的简单解释,该事件可以是 astringdouble. 提供的示例使用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_Pressureis redundant the sense that if I can pass the event directly to Pressure_Effectwhich 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.

我想做的Activate_Pressure是多余的感觉,如果我可以直接将事件传递给Pressure_Effect我不知道如何传递。我是 C# 新手,所以请耐心等待。我知道我没有启用计时器,并且此代码中可能缺少其他关键部分,但我只是发布它以明确我的问题。

采纳答案by C???

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 PressureEventa bit more. I would still keep a function that calculates von its own, given certain parameters, and then whatever else you need to do once you have vcould be its own method as well.

我不认为再分开PressureEvent一点不好。我仍然会保留一个v自己计算的函数,给定某些参数,然后一旦你拥有了你需要做的任何其他事情,它也v可以是它自己的方法。

回答by Servy

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:

所以你想忽略传递给给定事件处理程序的一些参数,并添加一些额外的固定值。您刚刚展示了如何做到这一点;您创建了 Timer 事件期望的签名的新方法,然后您省略了您不想要的参数并添加了一些您想要的固定值。您认为这是完成您想要的相当冗长的方法是正确的。确实有更简洁的语法。您可以使用匿名方法(更具体地说是 lambda)来执行您想要的操作。这是语法:

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