From 42afd92370c176df586a3ff5828e10af71f619eb Mon Sep 17 00:00:00 2001
From: shangfengh <3495281661@qq.com>
Date: Tue, 24 Oct 2023 16:25:41 +0800
Subject: [PATCH] feat: :sparkles: add the SafeValue StartTime
---
logic/GameClass/GameObj/Map/Chest.cs | 4 +-
.../Utility/SafeValue/InTheRange.cs | 236 ++++++-----
.../Utility/SafeValue/TimeBased.cs | 380 +++++++++++++++++-
3 files changed, 505 insertions(+), 115 deletions(-)
diff --git a/logic/GameClass/GameObj/Map/Chest.cs b/logic/GameClass/GameObj/Map/Chest.cs
index aae11fa..bf4e833 100644
--- a/logic/GameClass/GameObj/Map/Chest.cs
+++ b/logic/GameClass/GameObj/Map/Chest.cs
@@ -18,7 +18,7 @@ namespace GameClass.GameObj
private readonly Gadget[] propInChest = new Gadget[GameData.maxNumOfPropInChest] { new NullProp(), new NullProp() };
public Gadget[] PropInChest => propInChest;
- private TimeBasedProgressForInterrupting openProgress = new TimeBasedProgressForInterrupting();
- public TimeBasedProgressForInterrupting OpenProgress { get => openProgress; }
+ private TimeBasedProgressOptimizedForInterrupting openProgress = new TimeBasedProgressOptimizedForInterrupting();
+ public TimeBasedProgressOptimizedForInterrupting OpenProgress { get => openProgress; }
}
}
diff --git a/logic/Preparation/Utility/SafeValue/InTheRange.cs b/logic/Preparation/Utility/SafeValue/InTheRange.cs
index 44595be..1437e58 100644
--- a/logic/Preparation/Utility/SafeValue/InTheRange.cs
+++ b/logic/Preparation/Utility/SafeValue/InTheRange.cs
@@ -13,6 +13,7 @@ namespace Preparation.Utility
private int v;
private int maxV;
private readonly object vLock = new();
+ #region 构造与读取
public IntInTheVariableRange(int value, int maxValue)
{
if (maxValue < 0)
@@ -50,7 +51,10 @@ namespace Preparation.Utility
public int GetValue() { lock (vLock) return v; }
public static implicit operator int(IntInTheVariableRange aint) => aint.GetValue();
public int GetMaxV() { lock (vLock) return maxV; }
+ public bool IsMaxV() { lock (vLock) return v == maxV; }
+ #endregion
+ #region 普通设置MaxV与Value的值的方法
///
/// 若maxValue<=0则maxValue设为0并返回False
///
@@ -82,29 +86,7 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
- ///
- /// 如果当前值大于maxValue,则更新maxValue失败
- ///
- public bool TrySetMaxV(int maxValue)
- {
- lock (vLock)
- {
- if (v > maxValue) return false;
- maxV = maxValue;
- return true;
- }
- }
- ///
- /// 应当保证该value>=0
- ///
- public int SetPositiveV(int value)
- {
- lock (vLock)
- {
- return v = (value > maxV) ? maxV : value;
- }
- }
public int SetV(int value)
{
if (value <= 0)
@@ -119,6 +101,19 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
+ ///
+ /// 应当保证该value>=0
+ ///
+ public int SetPositiveV(int value)
+ {
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ #endregion
+
+ #region 普通运算
/// 返回实际改变量
public int AddV(int addV)
{
@@ -193,30 +188,26 @@ namespace Preparation.Utility
}
return subPositiveV;
}
+ #endregion
+ #region 特殊条件的设置MaxV与Value的值的方法
///
- /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ /// 如果当前值大于maxValue,则更新maxValue失败
///
- /// 返回实际改变量
- public int TryAddToMaxV(int addV)
+ public bool TrySetMaxV(int maxValue)
{
lock (vLock)
{
- if (maxV - v <= addV)
- {
- addV = maxV - v;
- v = maxV;
- return addV;
- }
- return -1;
+ if (v > maxValue) return false;
+ maxV = maxValue;
+ return true;
}
}
-
- public bool Set0IfNotMax()
+ public bool Set0IfNotMaxor0()
{
lock (vLock)
{
- if (v < maxV)
+ if (v < maxV && v > 0)
{
v = 0;
return true;
@@ -236,14 +227,27 @@ namespace Preparation.Utility
}
return false;
}
+ #endregion
- public bool IsMaxV()
+ #region 特殊条件的运算
+ ///
+ /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ ///
+ /// 返回实际改变量
+ public int TryAddToMaxV(int addV)
{
lock (vLock)
{
- return v == maxV;
+ if (maxV - v <= addV)
+ {
+ addV = maxV - v;
+ v = maxV;
+ return addV;
+ }
+ return -1;
}
}
+ #endregion
}
///
@@ -254,6 +258,7 @@ namespace Preparation.Utility
private long v;
private long maxV;
private readonly object vLock = new();
+ #region 构造与读取
public LongInTheVariableRange(long value, long maxValue)
{
if (maxValue < 0)
@@ -291,7 +296,16 @@ namespace Preparation.Utility
public long GetValue() { lock (vLock) return v; }
public static implicit operator long(LongInTheVariableRange aint) => aint.GetValue();
public long GetMaxV() { lock (vLock) return maxV; }
+ public bool IsMaxV()
+ {
+ lock (vLock)
+ {
+ return v == maxV;
+ }
+ }
+ #endregion
+ #region 普通设置MaxV与Value的值的方法
///
/// 若maxValue<=0则maxValue设为0并返回False
///
@@ -323,29 +337,7 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
- ///
- /// 如果当前值大于maxValue,则更新maxValue失败
- ///
- public bool TrySetMaxV(long maxValue)
- {
- lock (vLock)
- {
- if (v > maxValue) return false;
- maxV = maxValue;
- return true;
- }
- }
- ///
- /// 应当保证该value>=0
- ///
- public long SetPositiveV(long value)
- {
- lock (vLock)
- {
- return v = (value > maxV) ? maxV : value;
- }
- }
public long SetV(long value)
{
if (value <= 0)
@@ -360,6 +352,19 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
+ ///
+ /// 应当保证该value>=0
+ ///
+ public long SetPositiveV(long value)
+ {
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ #endregion
+
+ #region 普通运算
/// 返回实际改变量
public long AddV(long addV)
{
@@ -434,22 +439,19 @@ namespace Preparation.Utility
}
return subPositiveV;
}
+ #endregion
+ #region 特殊条件的设置MaxV与Value的值的方法
///
- /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ /// 如果当前值大于maxValue,则更新maxValue失败
///
- /// 返回实际改变量
- public long TryAddToMaxV(long addV)
+ public bool TrySetMaxV(long maxValue)
{
lock (vLock)
{
- if (maxV - v <= addV)
- {
- addV = maxV - v;
- v = maxV;
- return addV;
- }
- return -1;
+ if (v > maxValue) return false;
+ maxV = maxValue;
+ return true;
}
}
@@ -477,14 +479,27 @@ namespace Preparation.Utility
}
return false;
}
+ #endregion
- public bool IsMaxV()
+ #region 特殊条件的运算
+ ///
+ /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ ///
+ /// 返回实际改变量
+ public long TryAddToMaxV(long addV)
{
lock (vLock)
{
- return v == maxV;
+ if (maxV - v <= addV)
+ {
+ addV = maxV - v;
+ v = maxV;
+ return addV;
+ }
+ return -1;
}
}
+ #endregion
}
///
@@ -495,6 +510,7 @@ namespace Preparation.Utility
private double v;
private double maxV;
private readonly object vLock = new();
+ #region 构造与读取
public DoubleInTheVariableRange(double value, double maxValue)
{
if (maxValue < 0)
@@ -532,7 +548,16 @@ namespace Preparation.Utility
public double GetValue() { lock (vLock) return v; }
public static implicit operator double(DoubleInTheVariableRange adouble) => adouble.GetValue();
public double GetMaxV() { lock (vLock) return maxV; }
+ public bool IsMaxV()
+ {
+ lock (vLock)
+ {
+ return v == maxV;
+ }
+ }
+ #endregion
+ #region 普通设置MaxV与Value的值的方法
///
/// 若maxValue<=0则maxValue设为0并返回False
///
@@ -564,29 +589,7 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
- ///
- /// 如果当前值大于maxValue,则更新maxValue失败
- ///
- public bool TrySetMaxV(double maxValue)
- {
- lock (vLock)
- {
- if (v > maxValue) return false;
- maxV = maxValue;
- return true;
- }
- }
- ///
- /// 应当保证该value>=0
- ///
- public double SetPositiveV(double value)
- {
- lock (vLock)
- {
- return v = (value > maxV) ? maxV : value;
- }
- }
public double SetV(double value)
{
if (value <= 0)
@@ -601,6 +604,19 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
+ ///
+ /// 应当保证该value>=0
+ ///
+ public double SetPositiveV(double value)
+ {
+ lock (vLock)
+ {
+ return v = (value > maxV) ? maxV : value;
+ }
+ }
+ #endregion
+
+ #region 普通运算
/// 返回实际改变量
public double AddV(double addV)
{
@@ -675,22 +691,19 @@ namespace Preparation.Utility
}
return subPositiveV;
}
+ #endregion
+ #region 特殊条件的设置MaxV与Value的值的方法
///
- /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ /// 如果当前值大于maxValue,则更新maxValue失败
///
- /// 返回实际改变量
- public double TryAddToMaxV(double addV)
+ public bool TrySetMaxV(double maxValue)
{
lock (vLock)
{
- if (maxV - v <= addV)
- {
- addV = maxV - v;
- v = maxV;
- return addV;
- }
- return -1;
+ if (v > maxValue) return false;
+ maxV = maxValue;
+ return true;
}
}
@@ -718,13 +731,26 @@ namespace Preparation.Utility
}
return false;
}
+ #endregion
- public bool IsMaxV()
+ #region 特殊条件的运算
+ ///
+ /// 试图加到满,如果无法加到maxValue则不加并返回-1
+ ///
+ /// 返回实际改变量
+ public double TryAddToMaxV(double addV)
{
lock (vLock)
{
- return v == maxV;
+ if (maxV - v <= addV)
+ {
+ addV = maxV - v;
+ v = maxV;
+ return addV;
+ }
+ return -1;
}
}
+ #endregion
}
}
\ No newline at end of file
diff --git a/logic/Preparation/Utility/SafeValue/TimeBased.cs b/logic/Preparation/Utility/SafeValue/TimeBased.cs
index 566441c..7f34068 100644
--- a/logic/Preparation/Utility/SafeValue/TimeBased.cs
+++ b/logic/Preparation/Utility/SafeValue/TimeBased.cs
@@ -5,23 +5,47 @@ namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值
+ ///
+ /// 记录上次Start的时间,尚未Start则为long.MaxValue
+ /// 当前不为long.MaxValue则不能Start
+ ///
+ public class StartTime
+ {
+ private long _time;
+ public StartTime(long time)
+ {
+ _time = time;
+ }
+ public StartTime() { _time = long.MaxValue; }
+ public long Get() => Interlocked.CompareExchange(ref _time, -2, -2);
+ /// 返回操作前的值
+ public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
+ /// 返回操作前的值
+ public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
+ public void StopIfPassing(long passedTime)
+ {
+ if (Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2) > passedTime)
+ Interlocked.Exchange(ref _time, long.MaxValue);
+ }
+ }
+
///
/// 根据时间推算Start后完成多少进度的进度条(long)。
/// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
/// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
/// 通过原子操作实现。
///
- public class TimeBasedProgressForInterrupting
+ public class TimeBasedProgressOptimizedForInterrupting
{
private long endT = long.MaxValue;
private long needT;
- public TimeBasedProgressForInterrupting(long needTime)
+ public TimeBasedProgressOptimizedForInterrupting(long needTime)
{
- if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressForInterrupting.needTime (" + needTime.ToString() + ") is less than 0.");
+ if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
- public TimeBasedProgressForInterrupting()
+ public TimeBasedProgressOptimizedForInterrupting()
{
this.needT = 0;
}
@@ -32,7 +56,7 @@ namespace Preparation.Utility
{
return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
}
- public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue;
+ public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
///
/// GetProgress<0则表明未开始
///
@@ -68,7 +92,7 @@ namespace Preparation.Utility
///
/// <0则表明未开始
///
- public static implicit operator long(TimeBasedProgressForInterrupting pLong) => pLong.GetProgress();
+ public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();
///
/// GetProgressDouble<0则表明未开始
@@ -94,12 +118,12 @@ namespace Preparation.Utility
{
if (needTime <= 0)
{
- Debugger.Output("Warning:Start TimeBasedProgressForInterrupting with the needTime (" + needTime.ToString() + ") which is less than 0.");
+ Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
return false;
}
//规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
- if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressForInterrupting is " + needTime.ToString() + ",which is too small.");
+ if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
Interlocked.Exchange(ref needT, needTime);
return true;
}
@@ -127,6 +151,346 @@ namespace Preparation.Utility
}
//增加其他新的写操作可能导致不安全
}
+ /*
+ public class TimeBasedProgressAtVariableSpeed
+ {
+ private long progress = 0;
+ private long lastStartT = long.MaxValue;
+ private readonly object progressLock = new();
+ private long needProgress;
+
+ public TimeBasedProgressAtVariableSpeed(long needProgress)
+ {
+ if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
+ this.needProgress = needProgress;
+ }
+ public TimeBasedProgressAtVariableSpeed()
+ {
+ this.needProgress = 0;
+ }
+ public long GetNeedProgress()
+ {
+ lock (progressLock) return needProgress;
+ }
+ public override string ToString()
+ {
+ lock (progressLock)
+ {
+ return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
+ + "FinishedProgress:" + progress.ToString() +"ms, "
+ + "lastStartTime:" +lastStartT.ToString() +"ms";
+ }
+ }
+ public long GetProgressNow()
+ {
+ lock (progressLock)
+ {
+ if (lastStartT==long.MaxValue) return progress;
+ long progressNow = progress + Environment.TickCount64 - lastStartT;
+ if (progressNow>= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ return progress = needProgress;
+ }
+ return progressNow;
+ }
+ }
+
+ public bool IsFinished()
+ {
+ lock (progressLock)
+ {
+ if (progress >= needProgress) return true;
+ if (progress + Environment.TickCount64 - lastStartT >= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ progress = needProgress;
+ return true;
+ }
+ return false;
+ }
+ }
+ public bool IsProgressing()
+ {
+ lock (progressLock)
+ {
+ if (lastStartT==long.MaxValue) return false;
+ if (progress + Environment.TickCount64 - lastStartT >= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ progress = needProgress;
+ return false;
+ }
+ return true;
+ }
+ }
+
+ ///
+ /// GetProgress<0则表明未开始
+ ///
+ public long GetProgress()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ }
+ public long GetNonNegativeProgress()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
+ ///
+ /// GetProgress<0则表明未开始
+ ///
+ public long GetProgress(long time)
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ }
+ public long GetNonNegativeProgress(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
+ ///
+ /// <0则表明未开始
+ ///
+ public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
+
+ ///
+ /// GetProgressDouble<0则表明未开始
+ ///
+ public double GetProgressDouble()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return 1;
+ long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
+ if (needTime == 0) return 0;
+ return 1.0 - ((double)cutime / needTime);
+ }
+ public double GetNonNegativeProgressDouble(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return 1;
+ long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
+ if (needTime <= cutime) return 0;
+ return 1.0 - ((double)cutime / needTime);
+ }
+
+ public bool Start(long needTime)
+ {
+ if (needTime <= 2)
+ {
+ Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
+ return false;
+ }
+ //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
+ if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
+ Interlocked.Exchange(ref needProgress, needTime);
+ return true;
+ }
+ public bool Start()
+ {
+ return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
+ }
+ ///
+ /// 使进度条强制终止清零
+ ///
+ public void Set0()
+ {
+ Interlocked.Exchange(ref lastStartT, long.MaxValue);
+ Interlocked.Exchange(ref progress, 0);
+ }
+ ///
+ /// 使进度条暂停
+ ///
+ public bool Pause()
+ {
+ Interlocked.CompareExchange(ref lastStartT, -2, -2)
+ if (Environment.TickCount64 < )
+ {
+ Interlocked.Exchange(ref endT, long.MaxValue);
+ return true;
+ }
+ return false;
+ }
+ }*/
+
+ public class TimeBasedProgressAtVariableSpeed
+ {
+ private long progress = 0;
+ private long lastStartT = long.MaxValue;
+ private readonly object progressLock = new();
+ private long needProgress;
+ private double speed;
+
+ public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
+ {
+ if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
+ this.needProgress = needProgress;
+ this.speed = speed;
+ }
+ public TimeBasedProgressAtVariableSpeed()
+ {
+ this.needProgress = 0;
+ }
+ public long GetNeedProgress()
+ {
+ lock (progressLock) return needProgress;
+ }
+ public override string ToString()
+ {
+ lock (progressLock)
+ {
+ return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
+ + "FinishedProgress:" + progress.ToString() + "ms, "
+ + "lastStartTime:" + lastStartT.ToString() + "ms";
+ }
+ }
+ public long GetProgressNow()
+ {
+ lock (progressLock)
+ {
+ if (lastStartT == long.MaxValue) return progress;
+ long progressNow = progress + Environment.TickCount64 - lastStartT;
+ if (progressNow >= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ return progress = needProgress;
+ }
+ return progressNow;
+ }
+ }
+
+ public bool IsFinished()
+ {
+ lock (progressLock)
+ {
+ if (progress >= needProgress) return true;
+ if (progress + Environment.TickCount64 - lastStartT >= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ progress = needProgress;
+ return true;
+ }
+ return false;
+ }
+ }
+ public bool IsProgressing()
+ {
+ lock (progressLock)
+ {
+ if (lastStartT == long.MaxValue) return false;
+ if (progress + Environment.TickCount64 - lastStartT >= needProgress)
+ {
+ lastStartT = long.MaxValue;
+ progress = needProgress;
+ return false;
+ }
+ return true;
+ }
+ }
+
+ ///
+ /// GetProgress<0则表明未开始
+ ///
+ public long GetProgress()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ }
+ public long GetNonNegativeProgress()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
+ ///
+ /// GetProgress<0则表明未开始
+ ///
+ public long GetProgress(long time)
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ }
+ public long GetNonNegativeProgress(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
+ long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
+ return progress < 0 ? 0 : progress;
+ }
+ ///
+ /// <0则表明未开始
+ ///
+ public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
+
+ ///
+ /// GetProgressDouble<0则表明未开始
+ ///
+ public double GetProgressDouble()
+ {
+ long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
+ if (cutime <= 0) return 1;
+ long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
+ if (needTime == 0) return 0;
+ return 1.0 - ((double)cutime / needTime);
+ }
+ public double GetNonNegativeProgressDouble(long time)
+ {
+ long cutime = Interlocked.Read(ref endT) - time;
+ if (cutime <= 0) return 1;
+ long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
+ if (needTime <= cutime) return 0;
+ return 1.0 - ((double)cutime / needTime);
+ }
+
+ public bool Start(long needTime)
+ {
+ if (needTime <= 2)
+ {
+ Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
+ return false;
+ }
+ //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
+ if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
+ Interlocked.Exchange(ref needProgress, needTime);
+ return true;
+ }
+ public bool Start()
+ {
+ return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
+ }
+ ///
+ /// 使进度条强制终止清零
+ ///
+ public void Set0()
+ {
+ Interlocked.Exchange(ref lastStartT, long.MaxValue);
+ Interlocked.Exchange(ref progress, 0);
+ }
+ ///
+ /// 使进度条暂停
+ ///
+ public bool Pause()
+ {
+ Interlocked.CompareExchange(ref lastStartT, -2, -2)
+ if (Environment.TickCount64 < )
+ {
+ Interlocked.Exchange(ref endT, long.MaxValue);
+ return true;
+ }
+ return false;
+ }
+ }
///
/// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True