Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I creating a stop watch that tracks time, and when started accumulates time then when stopped waits then when started continues on from the previous point.

My problem is when start button is clicked it picks up at the current time it does not resume at the stopped point.

What am I doing wrong?

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WedStop
{
    public class TimerLogic
    {
    
        
        private DateTime startTime;
        private DateTime stopTime;
        private bool hasStarted = false;
        private bool hasStopped = false;

        public String ElapsedTime
        {
           
            get
            {
                if (hasStopped)
                {

                    return (DateTime.Now - stopTime).ToString();  
                }
                if (hasStarted) // good old code
                {                      
                    return (DateTime.Now - startTime).ToString();                    
                }
                return "00 HR:00 MN:00 SEC";
                }
        }
   

        public void StartClock()
        {
                if (hasStopped == false)
                { 
                   hasStarted = true; 
                   startTime = DateTime.Now; 
                }

                if (hasStopped == true)
                {
                    
                    hasStopped = true;
                }
                
                
        }

        public void StopClock() 
        {
            
            stopTime = startTime;
            hasStopped = true;
            hasStarted = false;
            
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WedStop
{
    public partial class Form1 : Form
    {
        TimerLogic stopWatch = new TimerLogic();

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblDisplay.Text = stopWatch.ElapsedTime;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            
            stopWatch.StartClock();
            timer1.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            stopWatch.StopClock();
            
          
        }
    }
}


What I have tried:

I think the problem is in the Timer logic class with this line of code:

C#
return (DateTime.Now - stopTime).ToString();
Posted
Updated 20-Oct-16 19:00pm
v3
Comments
Ehsan Sajjad 20-Oct-16 18:12pm    
why not use StopWatch class provided by .Net framework?
[no name] 20-Oct-16 18:14pm    
"this line of code", I don't think so. You should learn how to use the debugger sooner than yesterday. Then you would know which line of code is the culprit instead of just guessing.

1 solution

You can use the framwork provided StopWatch class, but if you are trying to learn and implement your own stopwatch, then your mistake is actually where you have pointed out yourself, you need to hold the :

C#
var difference = DateTime.Now - stopTime;
return String.Format("{0} HR:{1} MN:{2} SEC", diff.Hours,diff.Minutes,diff.Seconds));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900