@@ -5,23 +5,47 @@ namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值
/// <summary>
/// 记录上次Start的时间,尚未Start则为long.MaxValue
/// 当前不为long.MaxValue则不能Start
/// </summary>
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);
/// <returns>返回操作前的值</returns>
public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
/// <returns>返回操作前的值</returns>
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);
}
}
/// <summary>
/// 根据时间推算Start后完成多少进度的进度条(long)。
/// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
/// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
/// 通过原子操作实现。
/// </summary>
public class TimeBasedProgressForInterrupting
public class TimeBasedProgressOptimized ForInterrupting
{
private long endT = long.MaxValue;
private long needT;
public TimeBasedProgressForInterrupting(long needTime)
public TimeBasedProgressOptimized ForInterrupting(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 TimeBasedProgressOptimized ForInterrupting()
{
this.needT = 0;
}
@@ -32,7 +56,7 @@ namespace Preparation.Utility
{
return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
}
public bool IsOpen ed() => Interlocked.Read(ref endT) != long.MaxValue;
public bool IsStart ed() => Interlocked.Read(ref endT) != long.MaxValue;
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
@@ -68,7 +92,7 @@ namespace Preparation.Utility
/// <summary>
/// <0则表明未开始
/// </summary>
public static implicit operator long(TimeBasedProgressForInterrupting pLong) => pLong.GetProgress();
public static implicit operator long(TimeBasedProgressOptimized ForInterrupting pLong) => pLong.GetProgress();
/// <summary>
/// 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 TimeBasedProgressOptimized ForInterrupting 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;
}
}
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// <0则表明未开始
/// </summary>
public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
/// <summary>
/// GetProgressDouble<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// 使进度条强制终止清零
/// </summary>
public void Set0()
{
Interlocked.Exchange(ref lastStartT, long.MaxValue);
Interlocked.Exchange(ref progress, 0);
}
/// <summary>
/// 使进度条暂停
/// </summary>
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;
}
}
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// <0则表明未开始
/// </summary>
public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
/// <summary>
/// GetProgressDouble<0则表明未开始
/// </summary>
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;
}
/// <summary>
/// 使进度条强制终止清零
/// </summary>
public void Set0()
{
Interlocked.Exchange(ref lastStartT, long.MaxValue);
Interlocked.Exchange(ref progress, 0);
}
/// <summary>
/// 使进度条暂停
/// </summary>
public bool Pause()
{
Interlocked.CompareExchange(ref lastStartT, -2, -2)
if (Environment.TickCount64 < )
{
Interlocked.Exchange(ref endT, long.MaxValue);
return true;
}
return false;
}
}
/// <summary>
/// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True