Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to call a custom function when timer has elapsed

How to call a custom function when timer has elapsed

Scheduled Pinned Locked Moved C#
questionhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    edel_ong
    wrote on last edited by
    #1

    public static void DisplayTimeEvent( object source, ElapsedEventArgs e ) { if(source==myTimer) { frmRun.getDanceModel(); <-- this line gets an error message "denotes a field where a class is expected" } } how do I call any function when timer has elapsed? I don't quite understand why DisplayTimeEvent doesnt allow a function to be called.. Thanks much

    N 1 Reply Last reply
    0
    • E edel_ong

      public static void DisplayTimeEvent( object source, ElapsedEventArgs e ) { if(source==myTimer) { frmRun.getDanceModel(); <-- this line gets an error message "denotes a field where a class is expected" } } how do I call any function when timer has elapsed? I don't quite understand why DisplayTimeEvent doesnt allow a function to be called.. Thanks much

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      If FrmRun is the name of your class -as I guess-, then for sure you'd get this error. Did you instantiate your class first? Also the way you compare the source with the desired timer is quite odd. I guess you have multiple timers, and you have one handle for all of them? Otherwise if one timer only fires this event then no need to compare. A suggested version of the handler would be this

      public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
      {
      Timer test = source as Timer;
      if(test != null && test == myTimer)
      {
      frmRun MyForm = new frmRun();
      frmRun.getDanceModel();
      }
      }

      PS. You did declare your method correctly, didn't you? Did you successfully call it from another method?

      Regards:rose:

      E 1 Reply Last reply
      0
      • N Nader Elshehabi

        If FrmRun is the name of your class -as I guess-, then for sure you'd get this error. Did you instantiate your class first? Also the way you compare the source with the desired timer is quite odd. I guess you have multiple timers, and you have one handle for all of them? Otherwise if one timer only fires this event then no need to compare. A suggested version of the handler would be this

        public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
        {
        Timer test = source as Timer;
        if(test != null && test == myTimer)
        {
        frmRun MyForm = new frmRun();
        frmRun.getDanceModel();
        }
        }

        PS. You did declare your method correctly, didn't you? Did you successfully call it from another method?

        Regards:rose:

        E Offline
        E Offline
        edel_ong
        wrote on last edited by
        #3

        Thanks, however my problem is, frmRun has already been previously instantiated and what I want to do is call a function in frmRun which increments a value that is in frmRun. However the function doesnt recognize a variable that has been instantiated not in DisplayTimeEvent itself. How do I go about this problem? Thank you very much!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups