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
  1. Home
  2. General Programming
  3. C#
  4. problems getting a timer to work

problems getting a timer to work

Scheduled Pinned Locked Moved C#
csharphelpquestion
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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am having problems getting a simple timer to work. I am very new to C#. When I go to compile the code below I get the following error: An object reference is required for the nonstatic field, method, or property 'TimerTest.mStartTime' Anyone got any ideas ? using System; using System.IO; using System.Timers; class TimerTest { public DateTime mStartTime; public static void Main() { Timer tmr = new Timer(); tmr.Tick += new EventHandler(OnTimedEvent); tmr.Interval= 1000; DateTime mStartTime = DateTime.Now; Console.WriteLine("Timer test running..."); tmr.Enabled = true; while(true) { } } public static void OnTimedEvent(object source, EventArgs e) { String timestring; DateTime now = DateTime.Now; TimeSpan diff = now.Subtract(mStartTime); timestring = (diff.Hours.ToString("d2" + ":" + diff.Minutes.ToString("d2") + ":" + diff.Seconds.ToString("d2")); Console.WriteLine("{0}",timestring); } }

    J 1 Reply Last reply
    0
    • L Lost User

      I am having problems getting a simple timer to work. I am very new to C#. When I go to compile the code below I get the following error: An object reference is required for the nonstatic field, method, or property 'TimerTest.mStartTime' Anyone got any ideas ? using System; using System.IO; using System.Timers; class TimerTest { public DateTime mStartTime; public static void Main() { Timer tmr = new Timer(); tmr.Tick += new EventHandler(OnTimedEvent); tmr.Interval= 1000; DateTime mStartTime = DateTime.Now; Console.WriteLine("Timer test running..."); tmr.Enabled = true; while(true) { } } public static void OnTimedEvent(object source, EventArgs e) { String timestring; DateTime now = DateTime.Now; TimeSpan diff = now.Subtract(mStartTime); timestring = (diff.Hours.ToString("d2" + ":" + diff.Minutes.ToString("d2") + ":" + diff.Seconds.ToString("d2")); Console.WriteLine("{0}",timestring); } }

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      B. Wood wrote: public DateTime mStartTime; public static DateTime mStartTime; :) Since your methods are static, the data it accesses must be static, or have a reference to an instance of the class. James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

      L 1 Reply Last reply
      0
      • J James T Johnson

        B. Wood wrote: public DateTime mStartTime; public static DateTime mStartTime; :) Since your methods are static, the data it accesses must be static, or have a reference to an instance of the class. James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks James, that did the trick.

        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