//时间:20200611 //作者:郝爽 //功能:测量状态 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MeasureThread { //测试结果 public enum ThreadState { Ready, //就绪 InProcess, //进行中 Waiting, //等待,半自动化测试过程的状态 Failed, //失败 Success //成功 } //计算时间类型 public enum THREAD_TIME_TYPE { START = 1, STOPPED = 2, } public class ThreadStatus { //线程状态 private ThreadState m_threadState; public ThreadState STATE { get { return this.m_threadState; } set { this.m_threadState = value; } } //线程开始的时间 private DateTime m_startTime; public DateTime StartTime { get { return this.m_startTime; } set { this.m_startTime = value; } } //线程结束的时间 private DateTime m_endTime; public DateTime EndTime { get { return this.m_endTime; } set { this.m_endTime = value; } } //线程使用的时间 private TimeSpan m_UsedTime; public TimeSpan UsedTime { get { return this.m_UsedTime; } set { this.m_UsedTime = value; } } //线程当前开始的时间 private DateTime m_CurrentStartTime; public DateTime CurrentStartTime { get { return this.m_CurrentStartTime; } set { this.m_CurrentStartTime = value; } } //构造函数 public ThreadStatus() { Init(); } public void Init() { m_threadState = ThreadState.Ready; m_startTime = new DateTime(); m_endTime = new DateTime(); m_CurrentStartTime = new DateTime(); m_UsedTime = m_endTime - m_startTime; } // 计算时间 public bool ComputeTime(THREAD_TIME_TYPE a_nType) { if (a_nType == THREAD_TIME_TYPE.START) { if (m_startTime == new DateTime()) { m_startTime = DateTime.Now; m_CurrentStartTime = m_startTime; } else { m_CurrentStartTime = DateTime.Now; } m_endTime = DateTime.Now; } else if (a_nType == THREAD_TIME_TYPE.STOPPED) { // set current time as end time m_endTime = DateTime.Now; // compute used time TimeSpan timeUsed = new TimeSpan(); if (m_CurrentStartTime == m_startTime) { // first compute time timeUsed = m_endTime - m_startTime; } else { // not the first compute time timeUsed = m_UsedTime + ( m_endTime - m_CurrentStartTime); } m_UsedTime = timeUsed; } else { return false; } return true; } } }