Olivarsham
Olivarsham

Reputation: 1731

Calculating Time elapsed for ten users at the same time

I need to capture the time taken between two button press of ten users.
I am doing like this with StopWatch.

Stopwatch stopwatch1;  
Stopwatch stopwatch2;

........ Like this ten stop watches.

private void Start_Action1()  
{   
   stopwatch1 = new Stopwatch();  
   stopwatch1.Start();  
}  
private void Stop_Action1()  
{   
   stopwatch1.Stop();   
   txtTimeForAction1.Text = stopwatch.Elapsed.ToString();  
}

Same code for 10 StopWatches.
NOTE: All the users will do this START-STOP action continuously. I need to record time-elapsed for each cycle separately. I am using in desktop application. All the users will use the same application.

Using 10 Stopwatch is good practice?? Is there any better way than this?

Upvotes: 0

Views: 64

Answers (3)

Alex Anderson
Alex Anderson

Reputation: 860

Personally, I'd give each "user" a DateTime (StartTime) and then when the event has finished (So E.g. Key_Up) You can get the Elapsed time with:

DateTime elapsedTime = DateTime.Now - StartTime.

then use elapsedTime.Seconds or .Minutes etc. and even use elpasedTime.ToString("hh:mm:ss") to get a nicely formatted string.

Upvotes: 1

Philipp Sch
Philipp Sch

Reputation: 348

I see no reason why not using stop watches. But instead of defining ten stop watches you should save them in an array or in a dictionary where each StopWatch is associated with a user.

Upvotes: 1

Toon Casteele
Toon Casteele

Reputation: 2579

You could keep track of the starting times for every user, use one stopwatch and don't stop it after the stop action is called by one user, only when they have all stopped. I don't know if it's better practice, but it is a different way to do it.

Upvotes: 2

Related Questions