0

Title says it all i wanted to add a timer into my c# game emulator/application and wanted a timer to check the up time of this program meaning how long it has been running for but i want the timer to count up not down so i can check how many seconds it has been loaded. I have tried many tutorials but none work they just error.

I am adding the timer and the codes are not showing up to type in (if you get what i mean) once ive completed typing them they just error with red lines under them.

//uptime
        Timer timer = new Timer();
        timer.Interval = 60 * 1000;
        timer.Enabled = true;
        timer.tick();
        timer.Start();
Ashley Smith
  • 33
  • 1
  • 5
  • 1
    Indeed, we really need to see your code in order for us to help you. – Jordy Aug 08 '13 at 07:02
  • Ive added the code to the post, as you can see ive just done a simple tmer to grab seconds (not added the adding up not down) yet just a simple timer – Ashley Smith Aug 08 '13 at 07:06
  • "once ive completed typing them they just error with red lines under them." What is the error? – Jordy Aug 08 '13 at 07:13
  • 2
    If the suggested answer of Mark Bertenshaw is too dificult too understand, maybe you can use the Stopwatch class? – Koen Aug 08 '13 at 07:14
  • Ashley - as I said in my post, this is an unreliable mechanism. Timers aren't guaranteed to fire at regular intervals. In fact they are not guarenteed to fire at all. – Mark Bertenshaw Aug 08 '13 at 07:52

1 Answers1

2

You don't want to use a timer to work out uptime. The timer is too unreliable to expect it to fire exactly every second. So it's just as well that there is an API function you can use called GetProcessTimes():

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223%28v=vs.85%29.aspx

The PInvoke statement is:

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetProcessTimes(IntPtr hProcess, out FILETIME lpCreationTime, out FILETIME lpExitTime, out FILETIME lpKernelTime, out FILETIME lpUserTime);

Put this statement inside a class.

The imports you need for the compiler to find these types are as follows:

using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
using System.Runtime.InteropServices;

The function to convert FILETIME to DateTime is as follows:

    private DateTime FileTimeToDateTime(FILETIME fileTime)
    {
        ulong high = (ulong)fileTime.dwHighDateTime;
        unchecked
        {
            uint uLow = (uint)fileTime.dwLowDateTime;
            high = high << 32;
            return DateTime.FromFileTime((long)(high | (ulong)uLow));
        }
    }

Finally, a sample use of these two functions are as follows:

using System.Diagnostics;

void ShowElapsedTime()
{

        FILETIME lpCreationTime;
        FILETIME lpExitTime;
        FILETIME lpKernelTime;
        FILETIME lpUserTime;

        if (GetProcessTimes(Process.GetCurrentProcess().Handle, out lpCreationTime, out lpExitTime, out lpKernelTime, out lpUserTime))
        {
            DateTime creationTime = FileTimeToDateTime(lpCreationTime);
            TimeSpan elapsedTime = DateTime.Now.Subtract(creationTime);
            MessageBox.Show(elapsedTime.TotalSeconds.ToString());
        }
}
Mark Bertenshaw
  • 5,594
  • 2
  • 27
  • 40
  • I dont understan that code/program 1%, ive usless with c# just a begginer is there anything i can do with datetime? – Ashley Smith Aug 08 '13 at 07:07
  • @AshleySmith, if you are a beginner, you can use datetime and timespan to get uptime (but StopWatch is specifically designed for timing). I suspect you don't want microsecond accuracy. Please note this is far from best practice. But once you get more advanced, you'll find that PInvoke is really handy. www.pinvoke.net has lots of great examples. – Jordy Aug 08 '13 at 07:18
  • I've expanded my original post with code examples. Remember to group all the "using" statements together. The "using System.Diagnostics" statement is there to allow me to use the Process class. If you want to understand some of this, look up "PInvoke" and the reference on GetProcessTimes() that I supplied. – Mark Bertenshaw Aug 08 '13 at 07:50
  • @AshleySmith read this as well. Interesting views on Stopwatch http://kristofverbiest.blogspot.se/2008/10/beware-of-stopwatch.html – SteveB Aug 08 '13 at 09:29