C#datetime经过时间问题 [英] C# datetime elapsed time problem

查看:126
本文介绍了C#datetime经过时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个跟踪时间的秒表,当开始累积时间然后停止等待然后从上一点开始继续。



我的问题是当点击开始按钮时,它会在当前时间接收到它没有在停止点恢复。



我做错了什么?



 使用系统; 
使用 System.Collections.Generic;
使用 System.Diagnostics;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 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();
}
如果(hasStarted) // 好的旧代码
{
return (DateTime.Now - startTime).ToString();
}
return 00 HR:00 MN:00 SEC;
}
}


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

如果(hasStopped == true
{

hasStopped = true ;
}


}

public void StopClock()
{

stopTime = startTime;
hasStopped = true ;
hasStarted = false ;

}
}
}


使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

命名空间 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();


}
}
}





什么我试过了:



我认为问题出在Timer逻辑课程中,代码为:



  return (DateTime.Now  -  stopTime).ToString(); 

解决方案

您可以使用提供StopWatch类的框架,但如果您正在尝试学习并实现自己的秒表,那么您的错误实际上就是您自己指出的地方,需要持有:



  var 差异= DateTime.Now  - 停止时间; 
return String .Format( {0} HR:{1} MN:{2} SEC,diff.Hours,diff.Minutes,diff.Seconds));


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?

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:

return (DateTime.Now - stopTime).ToString();

解决方案

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 :

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


这篇关于C#datetime经过时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆