From 6d7fc472cd533a76e59c3ead9800b0acda6de788 Mon Sep 17 00:00:00 2001 From: youyc22 <125688164+youyc22@users.noreply.github.com> Date: Sat, 15 Jul 2023 01:28:13 +0800 Subject: [PATCH] Update Program.cs --- hw1/hw1/Program.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/hw1/hw1/Program.cs b/hw1/hw1/Program.cs index c20461d..2f81a6d 100644 --- a/hw1/hw1/Program.cs +++ b/hw1/hw1/Program.cs @@ -65,6 +65,62 @@ public interface ILongProgressByTime public class LongProgressByTime: ILongProgressByTime { + private long startTime; // 进度条开始时间 + private long needTime; // 当前Progress完成所需时间 + private bool isTerminated; // 进度条是否已终止 + + public LongProgressByTime() + { + startTime = 0; + needTime = 0; + isTerminated = true; + } + + public bool Start(long NeedTime) + { + if (isTerminated) + { + this.needTime = NeedTime; + startTime = Environment.TickCount64; + isTerminated = false; + return true; + } + else + { + return false; + } + } + + public bool TrySet0() + { + if (!isTerminated) + { + isTerminated = true; + return true; + } + else + { + return false; + } + } + + public void Set0() + { + isTerminated = true; + } + + public (long ElapsedTime, long NeedTime) GetProgress() + { + if (isTerminated) + { + return (0, 0); + } + else + { + long elapsedTime = Environment.TickCount64 - startTime; + return (elapsedTime, needTime); + } + } // 根据时间推算Start后完成多少进度的进度条(long)。 // 只允许Start时修改needTime(确保较大); // 支持TrySet0使未完成的进度条终止清零; @@ -87,4 +143,4 @@ public class LongProgressByTime: ILongProgressByTime A Progress: (516, 1000) Now: 14537078 A TrySet0: False */ -} \ No newline at end of file +}