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.

leppie
  • 115,091
  • 17
  • 196
  • 297
C126
  • 31
  • 3
  • 1
    Possible duplicate of [Retrieve system uptime using C#](http://stackoverflow.com/questions/972105/retrieve-system-uptime-using-c-sharp) – PoX Feb 04 '16 at 20:20
  • That is all similar to this, about the TickCount, but I want it to show in _minutes_. @PoX – C126 Feb 04 '16 at 20:23

3 Answers3

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.

Roujo
  • 503
  • 4
  • 12
  • 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 Feb 04 '16 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 Feb 04 '16 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 Feb 04 '16 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 Feb 04 '16 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 Feb 04 '16 at 21:13
  • @C126 You can either send `uptime.ToString()` directly, or you can put in the two lines I've mentioned in my answer to answer with just the number of minutes, yes. I've corrected the code to go from `uptime.Minutes` to `uptime.TotalMinutes`, which gives you what you want. =) – Roujo Feb 04 '16 at 21:16
  • Yes your code would work but the variable you used `serverStartTime`, I would need to put that in the program.cs in my source code (program.cs handles what the server loads and what it does when it first starts) and I dont know how I can use a variable in command.cs if the variable is in ANOTHER file. – C126 Feb 04 '16 at 21:29
  • @C126 Ah, got it. I've added an example of how to do that in my answer. If you have any further questions, you should probably post another questions - I might be able to help you if you link it here, too. =) – Roujo Feb 04 '16 at 21:34
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.

Carlos
  • 5,991
  • 6
  • 43
  • 82
  • 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 Feb 04 '16 at 20:41
0

Use a TimeSpan.

TimeSpan uptime = TimeSpan.FromMilliseconds(Environment.TickCount);
double totalMinutes = uptime.TotalMinutes;
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445