0

My code on Windows used P/Invoke to call GetTickCount64() from kernel32.dll This doesn't work under Mono on Ubuntu Server 14 - I get EntryPointNotFoundException.

There's this approach using PerformanceCounter. It works on Windows but when I use it on Linux with counter name/category set to System/System Up Time it yields zero timespans all the time. So it doesn't work too.

Environment.Tickount works the same as on Windows but it overflows every 47 days and this can get confusing results for my usecase, so I'd prefer a 64-bit counter.

How do I get system uptime in C# code running with Mono on Ubuntu Server?

5
  • Use native Linux way then, stackoverflow.com/questions/1540627/…
    – Lex Li
    Commented Feb 7, 2017 at 15:13
  • @LexLi How do I start with doing that in C# code?
    – sharptooth
    Commented Feb 7, 2017 at 15:15
  • Google how to do PInvoke on Mono. No much difference compared to what you do on Windows.
    – Lex Li
    Commented Feb 7, 2017 at 15:15
  • @LexLi That sounds too hard. I tried to File.ReadAllText("/proc/uptime") as outlined in one of the answers and it works just fine. Thank you.
    – sharptooth
    Commented Feb 7, 2017 at 15:19
  • Then post that as an answer and accept it.
    – Lex Li
    Commented Feb 7, 2017 at 15:42

1 Answer 1

1

User Lex Li linked to this closely related question for C code.

Among the answers is this one which suggets reading from /proc/uptime. This is just great for C# code - it can use File.ReadAllText() and then parse the resulting string.

var uptimeText = File.ReadAllText( "/proc/uptime" );
// Now split the string to extract the first component,
// parse it as double and use TimeSpan.FromSeconds()
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.