does a static timer works like any other static variable?

Hi everybody

I have a class Graph that has a static timer timSecs and a function start that enables the timer

Graph.timSecs.Enabled = true;

Graph.timSecs.Start( );

In the main application I have two objects of the Graph class. But it seems like the event timer just triggers for one of the objects.

Any suggestions Thanx



Answer this question

does a static timer works like any other static variable?

  • Sorin Solescu

    I learned on a project I was doing that the timer class needs to be the main working class. Your program class can call the timer but your code won't allow the timer to go off unless it is the base class.

    SO the thing to do is register from the timer class, and maybe use if or case statements to figure out which graph caused the timer to go off...



  • jjones11

    Well, I'm not sure how you'd create a static timer, unless the event it fires is also static.

  • Ivan Andrade

    Unfortunately my timer event make reference to properties of the class that are not static :( So I can't declare the event as static.

    private void timSecs_Tick(object sender, System.EventArgs e)

    {

    System.Windows.Forms.Timer t=(System.Windows.Forms.Timer)sender;

    this.tsecs =(((double)t.Interval/1000)+this.tsecs);

    ....

    }

    Maybe I would face this problem from another perspective: make another class and register in the graph class, like the observer pattern.

    Any suggestion


  • Chuck McD

    How about using a timer class

    Instead of having your class start the timer, it would be better to have the timer class call your classes...

    Something like this - I didn't test this, I know it works with one event handler, it should work with two. If it doesn't work, then create two timer classes, each only having one event handler. I prefer using Appsettings instead of hard coding the time...

    class myTimer

    {

    public static void SetTimer()

    {

    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed+=
    new ElapsedEventHandler(OnTimedEvent1);
    aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent2);

    aTimer.Interval=Convert.ToInt32(ConfigurationSettings.AppSettings "MYTIMERSETTINGl"]);
    aTimer.Enabled=
    true;
    while(Console.Read()!='q');

    }

    private static void OnTimedEvent1(object source, ElapsedEventArgs e)

    {

    Class1 C1 = new Class1();
    C1.RUNMYAPP();

    }

    private static void OnTimedEvent2(object source, ElapsedEventArgs e)

    {

    Class2 C2 = new Class2();
    C2.RUNMYAPP();

    }

    }

     


  • Gladys27216

    Hi Ken... thx for your suggestion. The problem with it is that I don't know how many objects of the Graph type I will use... It suppose I can add many objects of this class to the same form. And they all draw different functions in "real time" (aprox). So if I add a third Graph object I should edit the timer class and add a third event maybe I can "register" the graph objects in the timer class...what do u think

    LibertadV


  • does a static timer works like any other static variable?