-2

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
  • You're gonna want to take this request into a chatroom at the very least. – Mike G Aug 20 '13 at 14:07
  • 2
    [here is your example](http://msdn.microsoft.com/de-de/library/system.timers.timer(v=vs.80).aspx) – WiiMaxx Aug 20 '13 at 14:07
  • Can you show the code you use to set up and fire the event? What you want to do is possible, but you need to lay some groundwork for us. See [this thread](http://stackoverflow.com/questions/8644253/pass-parameter-to-eventhandler) for an example *not* tailored to your specific situation. – Cᴏʀʏ Aug 20 '13 at 14:45
  • @Cory: I have added a code to make this clear – Jack_of_All_Trades Aug 20 '13 at 14:52
  • @Jack_of_All_Trades: Is `pressure` always `2.5`, or where does that come from? Which parameter(s) represent(s) the amount of time that has elapsed? Do you need the time elapsed in milliseconds? – Cᴏʀʏ Aug 20 '13 at 15:37
  • @Cory: That is just what I put to show what I was doing. In my Actual program, It will be a weighted average of time-interval that has elapsed in some scale. Pressure is actually, in my program, a private member of the class declared double. The time elapsed, I was thinking of using some property of the Timer object which I have not yet researched fully, but at the meantime, 70 which I have passed to Pressure_Effect, I think would be fine because that will represent 70 seconds interval. I still need to find the better way to get that. – Jack_of_All_Trades Aug 20 '13 at 15:40
  • What are the units of time you need from the timer? The event that fires provides a `SignalTime` property which is a `DateTime` struct and contains the exact date the event fired. Some extra work may need to go into finding out how much time as elapsed, if you're starting from `0`, for example. – Cᴏʀʏ Aug 20 '13 at 15:44
  • Ok, so you need seconds. `t` will be a number of seconds, `pressure` is a constant or other value coming from a private variable in your class, and you want to do some sort of work every 70 milliseconds, is that correct? – Cᴏʀʏ Aug 20 '13 at 15:46
  • @Cory: I need something in milliseconds. However, Since the event is fired every 70 milliseconds, I can still use 70 milliseconds hard-coded. Not a good choice though but can get me going till I become more proficient with the language. – Jack_of_All_Trades Aug 20 '13 at 15:46
  • @Cory: Yes, that is right. – Jack_of_All_Trades Aug 20 '13 at 15:46
  • Last questions, I think: what is the mechanism for calling `Time_Handle`? Do you need the timer to start/stop based on something external happening? – Cᴏʀʏ Aug 20 '13 at 15:50
  • @Cory: I am writing this for one of my friend's game (Logic for Fluid Mechanics). The thing is, I would like to start the Time_Handle when I receive a signal from a button and would like to stop when somebody releases the button. But I think, that should not be my concern. I might be wrong though. – Jack_of_All_Trades Aug 20 '13 at 15:54

2 Answers2

5

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. 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ᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
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);
Servy
  • 202,030
  • 26
  • 332
  • 449