利用系统的简单例子。计时器。定时器在C# [英] Simple example of the use of System. Timers. Timer in C#

查看:174
本文介绍了利用系统的简单例子。计时器。定时器在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了MSDN网站和一切,但我不能找到如何提高它接受的参数可以是字符串或<$ C $定时事件容易解释C>双。所提供的例子使用 ElapsedEventArgs ,但没有说明实现我自己的参数来引发的事件的一个好方法。

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);

    }



我想要做的就是 Activate_Pressure 是多余的感觉,如果我能直接传递事件 Pressure_Effect ,我不知道怎么办。我是新来的C#,所以请多多包涵。我知道我没有启用定时器和其他关键部件可能会丢失在此代码,但我只是贴吧,以我的问题清楚。

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:

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)));
    }
}



我不认为这是坏分裂 PressureEvent 多一点。我仍然会保持这种计算 v 自身的功能,给予一定的参数,然后其他任何你需要做的,一旦你有 v 可能是其自己的方法为好。

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.

这篇关于利用系统的简单例子。计时器。定时器在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆