Stopwatch time per user in c # asp.net mvc application

I have a set of users in my asp.net mvc application.

When a user starts a stopwatch, I need a stopwatch to continue working even after the user logs out. And run it until it logs in again and stops.

I can run it when I click the start button

        stopWatch = new Stopwatch();
        stopWatch.Start();

But how can I track it against the user? so after the user logs out and logs in, he will see the elapsed time, including the time during which he was not active in the application?

therefore, after the user logs in and press the stop button, he should receive the elapsed time, including all the time.

TimeSpan ts = stopWatch.Elapsed;

is it better to use the angular directive / javascript timer?

+4
source share
1 answer

Why use Stopwatchfor something like that? Stopwatchdesigned to measure high resolution time, I doubt your user needs to know the exact time up to a few nanoseconds ...
From MSDN:

Provides a set of methods and properties that can be used to accurately measure elapsed time.

Just save the DateTime.Now()button state (for example, start, stop) and user ID, and then when the user with the same identifier presses this button again, simply calculate the time difference.

TimeSpan elapsedTime = FirstClickDateTime - SecondClickDateTime;

Then all you have to do is format display TimeSpan.

+1
source

All Articles