Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csharp_尤忆晨_无23 #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion hw1/hw1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ public interface ILongProgressByTime

public class LongProgressByTime: ILongProgressByTime
{
private long startTime; // 进度条开始时间
private long needTime; // 当前Progress完成所需时间
private bool isTerminated; // 进度条是否已终止
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有采用任何锁或者原子操作保证线程安全


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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能是题目表述不清楚,当进度条时间满时应当视为终止

{
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使未完成的进度条终止清零;
Expand All @@ -87,4 +143,4 @@ public class LongProgressByTime: ILongProgressByTime
A Progress: (516, 1000) Now: 14537078
A TrySet0: False
*/
}
}