none
Two status find the time I been in each RRS feed

  • Question

  • The above sounds easy if I wasn't going back and forth between status and you don't want to add a timer running constantly in the background.

    I've worked on this for a few days now trying here and there (a lot) to come up with solutions. I feel like today I'm making some real progress but I'm coming down to a deadline. I just need someone to help me figure it out in case I don't myself soon and so I'll explain how everything you need to know. 

    I have 2 buttons, 2 textboxes, and 1 data grid view that are important here. Button 1 is Ready, button 2 is Not Ready, textbox 1 is the amount of time in the Ready state, textbox 2 is the amount of time is the Not Ready state, and the data grid view is a spot I store the time that I entered and left each state. These buttons are binary and what I mean is you can only be one or the other. From here I need to be able to switch back and forth and only the amount of time I'm in a state update in that relative textbox. 

    Any help is certainly highly appreciated! Thank you!


    • Edited by fatalerror0x00 Tuesday, June 19, 2018 6:51 PM Realized I left out information that I hope makes a solution easier to find.
    Tuesday, June 19, 2018 5:27 PM

Answers

  • A stopwatch isn't an active timer. It snapshots the start time. Every time you request the elapsed time it calculates the differences between now and the start time (actually the ticks). This is the same logic you'd be using in your own code. Stopwatch relies on the high performance multimedia timers in Windows and is the most accurate timer available. Other than about 17 bytes of data for storing the start time it has no overhead. Start just sets the start time and stop just captures the difference.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 19, 2018 8:31 PM
    Moderator

All replies

  • Let's assume that you already have the logic working to toggle between status 1 and 2. Then one approach would be to add a stopwatch to each status. Each time you "move into" that status then resume the stopwatch. Each time you "move out" of that status then pause the stopwatch. At any point in time you can determine how long you've been in the status by looking at the stopwatch. This would basically mandate that you keep track of your "status" using a class or something instead of simply tracking a Boolean variable or something because you need to associate a stopwatch value with it.

    There are alternative approaches as well if you can have any # of statuses.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 19, 2018 8:06 PM
    Moderator
  • So I did mention I didn't want any active timer running constantly. I want to use a mathematical operation at the time the status changes to find how long you were in that status. It's a completely valid solution but I'm looking to find as we go when the status changes rather then something working in the background constantly. Thank you for this though!

    Tuesday, June 19, 2018 8:18 PM
  • A stopwatch isn't an active timer. It snapshots the start time. Every time you request the elapsed time it calculates the differences between now and the start time (actually the ticks). This is the same logic you'd be using in your own code. Stopwatch relies on the high performance multimedia timers in Windows and is the most accurate timer available. Other than about 17 bytes of data for storing the start time it has no overhead. Start just sets the start time and stop just captures the difference.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 19, 2018 8:31 PM
    Moderator
  • Okay I didn't realize that I'm sorry. That doesn't sound like it would be a solution that I'm unwilling to use. Storing data is no issue just like I said didn't want something running constantly in the background but if this won't then that's perfect I will attempt this in the morning when I'm able to take some time and implement that. Hopefully will have that in before noon! That'll make the boss happy!!!
    Wednesday, June 20, 2018 2:29 AM
  • Hi fatalerror0x00,

    Or you could try to log the time when you change the status. There is no need to do anything in the background. According to the time you logged, you could use Timespan to calculate the time interval.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 20, 2018 7:56 AM
    Moderator
  • Originally that was what I was trying to do but between all the conversions of data types I was struggling I might be able to do it now if I really wanted to take the time to but what you gave me works just fine and does everything pretty much for me I had to do one thing I didn't like to make it work but I'll figure out a way around that if possible which I think I can do I just first really cared about seeing if I could get it to work and I did this morning have it working just after my shift started. I eventually got a bunch of things going with this and it worked flawlessly.

    Wednesday, June 20, 2018 9:32 PM