2

So I run some type of game, and I want to add a command !uptime that displays how long server has been running for since the last open or whatever

This code (from microsoft website) shows the tick count and displays it correctly

        int result = Environment.TickCount & Int32.MaxValue;
        player.SendMessage("Result: " + result);

but I want to be able to display how long its been up in minutes.

2
  • 1
    Possible duplicate of Retrieve system uptime using C# Commented Feb 4, 2016 at 20:20
  • That is all similar to this, about the TickCount, but I want it to show in minutes. @PoX
    – C126
    Commented Feb 4, 2016 at 20:23

3 Answers 3

2

From the MSDN documentation, we can see that Environment.TickCount

Gets the number of milliseconds elapsed since the system started.

You can then convert it to minutes like so:

var minutes = (Environment.TickCount - serverStartTickCount) / 60000; // 1000 ms/s * 60 s/m

Alternatively, you might want to consider storing DateTime.Now when the server starts. Say your class is called Program, you can add this to it:

public static readonly DateTime ServerStartTime = DateTime.Now;

and then do this when the command is run:

var uptime = DateTime.Now - Program.ServerStartTime;
var minutes = uptime.TotalMinutes;

This would allow you to get an accurate uptime when the Environment.TickCount roll over every few weeks, as @Carlos pointed out.

8
  • For the first minute it has been up and I used it, it displayed "7891" and then the 2nd minute I did it it displayed "7892".
    – C126
    Commented Feb 4, 2016 at 20:38
  • @C126 Are you looking for the uptime of the OS or of your game server? I'm assuming it's the latter - I've written the code accordingly. =)
    – Roujo
    Commented Feb 4, 2016 at 20:42
  • I think its a problem on my side, I think its showing the uptime of my computer or something Idk lol is there a way to find out how long a program has been up if that program is running on another computer? like a vps for example since thats what I use to host the game @Roujo
    – C126
    Commented Feb 4, 2016 at 20:44
  • @C126 If you can modify that program, then you can look at the second part of my post: have it store the current date and time when starting up, and then use that to respond to the !uptime command. =)
    – Roujo
    Commented Feb 4, 2016 at 20:50
  • Ok, how can I make a variable var start = DateTime.Now in the servers program.cs and then be able to use it in command.cs so I can do this var uptime = DateTime.Now - start; player.SendMessage("Server Uptime: " + uptime.ToString()); or should I make new question for this.
    – C126
    Commented Feb 4, 2016 at 21:13
0

From the reference docs:

A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.

So divide by 1000 to get seconds, and then 60 to get minutes.

Note the thing is only 32 bit, so it loops back every few weeks.

1
  • The person below commented code that did what you said, and again I comment "For the first minute it has been up and I used it, it displayed "7891" and then the 2nd minute I did it it displayed "7892"." I don't know why. And the game isn't very popular, it restarts every day or so anyways so I don't mind about the looping.
    – C126
    Commented Feb 4, 2016 at 20:41
0

Use a TimeSpan.

TimeSpan uptime = TimeSpan.FromMilliseconds(Environment.TickCount);
double totalMinutes = uptime.TotalMinutes;

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