I was wondering if i could get a little help. I am working on a WPF application and i have to keep track how long it takes a user to make a report(make a report is one of the buttons), want the amount of time elapsed between when the XAML view is first shown (loaded) to the user and when the user clicks the 'Submit' button
-
1To be clear, you want the amount of time elapsed between when the XAML view is first shown (loaded) to the user and when the user clicks the 'Submit' button? – Mark Harrington Feb 05 '20 at 19:09
-
in the initialization of the window, set a datetime value and then when the submit is clicked, look at the elapsed time.. Have you even tried anything? – Kevin Cook Feb 05 '20 at 19:10
-
yes thats exactly what im looking for – LuDevGon Feb 05 '20 at 19:14
-
mark Harrington – LuDevGon Feb 05 '20 at 19:14
-
@MarkHarrington do you have a solution? – LuDevGon Feb 05 '20 at 19:37
1 Answers
This is how I would approach it. Define 2 private member variable of type Datetime. Let's name those variables startTime and endTime. Wire-up the XAML Window's Loaded event. In the Loaded event set the startTime = DateTime.Now. In the event handler for the 'Submit' button set endTime = DateTime.Now. To get the total elapsed time simply subtract the startTime from the endTime (var elapsedTime = endTime - startTime). The process should be able to be repeated unless this is the main control of your application and is always in scope. So, if this is your main Window that stays open you'll need a way to set the startTime for each subsequent operation. Maybe re-set the startTime right after you retrieve the elapsed time. Hope this helps!

- 308
- 4
- 10