C# Console Application timers -


title says wanted add timer c# game emulator/application , wanted timer check time of program meaning how long has been running want timer count not down can check how many seconds has been loaded. have tried many tutorials none work error.

i adding timer , codes not showing type in (if mean) once ive completed typing them error red lines under them.

//uptime         timer timer = new timer();         timer.interval = 60 * 1000;         timer.enabled = true;         timer.tick();         timer.start(); 

you don't want use timer work out uptime. timer unreliable expect fire every second. it's there api function 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 statement inside class.

the imports need compiler find these types follows:

using filetime = system.runtime.interopservices.comtypes.filetime; using system.runtime.interopservices; 

the function convert filetime datetime 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, sample use of these 2 functions 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());         } } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -