From 873135543c49239c36ed40c55aaa4dc664d902bb Mon Sep 17 00:00:00 2001
From: Grange <2634070476@qq.com>
Date: Fri, 21 Jul 2023 22:00:45 +0800
Subject: [PATCH] =?UTF-8?q?CSharp=5F=E9=A1=BE=E6=9C=97=E5=93=B2=5F?=
=?UTF-8?q?=E6=97=A021?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
hw1/.vscode/launch.json | 26 +++++
hw1/.vscode/tasks.json | 41 ++++++++
hw1/hw1/Program.cs | 225 ++++++++++++++++++++++++----------------
hw1/store.txt | 139 +++++++++++++++++++++++++
4 files changed, 344 insertions(+), 87 deletions(-)
create mode 100644 hw1/.vscode/launch.json
create mode 100644 hw1/.vscode/tasks.json
create mode 100644 hw1/store.txt
diff --git a/hw1/.vscode/launch.json b/hw1/.vscode/launch.json
new file mode 100644
index 0000000..4661127
--- /dev/null
+++ b/hw1/.vscode/launch.json
@@ -0,0 +1,26 @@
+{
+ "version": "0.2.0",
+ "configurations": [
+ {
+ // Use IntelliSense to find out which attributes exist for C# debugging
+ // Use hover for the description of the existing attributes
+ // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md
+ "name": ".NET Core Launch (console)",
+ "type": "coreclr",
+ "request": "launch",
+ "preLaunchTask": "build",
+ // If you have changed target frameworks, make sure to update the program path.
+ "program": "${workspaceFolder}/hw1/bin/Debug/net6.0/hw1.dll",
+ "args": [],
+ "cwd": "${workspaceFolder}/hw1",
+ // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
+ "console": "internalConsole",
+ "stopAtEntry": false
+ },
+ {
+ "name": ".NET Core Attach",
+ "type": "coreclr",
+ "request": "attach"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/hw1/.vscode/tasks.json b/hw1/.vscode/tasks.json
new file mode 100644
index 0000000..4fe1052
--- /dev/null
+++ b/hw1/.vscode/tasks.json
@@ -0,0 +1,41 @@
+{
+ "version": "2.0.0",
+ "tasks": [
+ {
+ "label": "build",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "build",
+ "${workspaceFolder}/hw1/hw1.csproj",
+ "/property:GenerateFullPaths=true",
+ "/consoleloggerparameters:NoSummary"
+ ],
+ "problemMatcher": "$msCompile"
+ },
+ {
+ "label": "publish",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "publish",
+ "${workspaceFolder}/hw1/hw1.csproj",
+ "/property:GenerateFullPaths=true",
+ "/consoleloggerparameters:NoSummary"
+ ],
+ "problemMatcher": "$msCompile"
+ },
+ {
+ "label": "watch",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "watch",
+ "run",
+ "--project",
+ "${workspaceFolder}/hw1/hw1.csproj"
+ ],
+ "problemMatcher": "$msCompile"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/hw1/hw1/Program.cs b/hw1/hw1/Program.cs
index b89423d..93a8e25 100644
--- a/hw1/hw1/Program.cs
+++ b/hw1/hw1/Program.cs
@@ -1,88 +1,139 @@
-using System;
-using System.Diagnostics;
-
-namespace Homework
-{
- public class Program
- {
- public static void Main(string[] args)
- {
- ILongProgressByTime a = new LongProgressByTime();
- new Thread
- (
- () =>
- {
- Console.WriteLine("A Start: "+(a.Start(2000)).ToString());
- Thread.Sleep(1000);
- Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
- Thread.Sleep(500);
- Console.WriteLine("A Start: "+(a.Start(1000)).ToString() +" Now: "+Environment.TickCount64);
- Thread.Sleep(500);
- Console.WriteLine("A Progress: "+(a.GetProgress()).ToString() +" Now: "+Environment.TickCount64);
- Thread.Sleep(1003);
- Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
- }
- ).Start();
-
- new Thread
- (
- () =>
- {
- Console.WriteLine("B Start: "+(a.Start(2000)).ToString());
- Thread.Sleep(1500);
- Console.WriteLine("B Start: " +(a.Start(1000)).ToString() + " Now: " + Environment.TickCount64);
- Thread.Sleep(500);
- Console.WriteLine("B Progress: " +(a.GetProgress()).ToString() + " Now: " + Environment.TickCount64);
- }
- ).Start();
- }
- }
-
- public interface ILongProgressByTime
- {
- ///
- /// 尝试加载下一次进度条,needTime指再次加载进度条所需时间,单位毫秒
- /// 如果之前进度条处于就绪态,则将进度开始下一次加载,返回true
- /// 如果之前进度条不处于就绪态,返回false
- ///
- public bool Start(long needTime);
-
- ///
- /// 使未完成的进度条清零并终止变为就绪态,返回值代表是否成功终止
- ///
- public bool TrySet0();
-
- ///
- /// 使进度条强制清零并终止变为就绪态
- ///
- public void Set0();
-
- ///
- /// ElapsedTime指其中已过去的时间,NeedTime指当前Progress完成所需时间,单位毫秒
- ///
- public (long ElapsedTime, long NeedTime) GetProgress();
- }
-
- public class LongProgressByTime: ILongProgressByTime
- {
- // 根据时间推算Start后完成多少进度的进度条(long)。
-
- // 只允许修改LongProgressByTime类中的代码
- // 要求实现ILongProgressByTime中的要求
- // 可利用Environment.TickCount64获取当前时间(单位ms)
-
- //挑战:利用原子操作
- //long.MaxValue非常久
- }
-
-/*输出示例(仅供参考):
- * A Start: False
-B Start: True
-A TrySet0: True
-B Start: True Now: 14536562
-A Start: False Now: 14536578
-B Progress: (516, 1000) Now: 14537078
-A Progress: (516, 1000) Now: 14537078
-A TrySet0: False
-*/
+using System;
+using System.Diagnostics;
+
+namespace Homework
+{
+ public class Program
+ {
+ public static void Main(string[] args)
+ {
+ ILongProgressByTime a = new LongProgressByTime();
+ new Thread
+ (
+ () =>
+ {
+ Console.WriteLine("A Start: "+(a.Start(2000)).ToString());
+ Thread.Sleep(1000);
+ Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
+ Thread.Sleep(500);
+ Console.WriteLine("A Start: "+(a.Start(1000)).ToString() +" Now: "+Environment.TickCount64);
+ Thread.Sleep(500);
+ Console.WriteLine("A Progress: "+(a.GetProgress()).ToString() +" Now: "+Environment.TickCount64);
+ Thread.Sleep(1003);
+ Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
+ }
+ ).Start();
+
+ new Thread
+ (
+ () =>
+ {
+ Console.WriteLine("B Start: "+(a.Start(2000)).ToString());
+ Thread.Sleep(1500);
+ Console.WriteLine("B Start: " +(a.Start(1000)).ToString() + " Now: " + Environment.TickCount64);
+ Thread.Sleep(500);
+ Console.WriteLine("B Progress: " +(a.GetProgress()).ToString() + " Now: " + Environment.TickCount64);
+ }
+ ).Start();
+ }
+ }
+
+ public interface ILongProgressByTime
+ {
+ ///
+ /// 尝试加载下一次进度条,needTime指再次加载进度条所需时间,单位毫秒
+ /// 如果之前进度条处于就绪态,则将进度开始下一次加载,返回true
+ /// 如果之前进度条不处于就绪态,返回false
+ ///
+ public bool Start(long needTime);
+
+ ///
+ /// 使未完成的进度条清零并终止变为就绪态,返回值代表是否成功终止
+ ///
+ public bool TrySet0();
+
+ ///
+ /// 使进度条强制清零并终止变为就绪态
+ ///
+ public void Set0();
+
+ ///
+ /// ElapsedTime指其中已过去的时间,NeedTime指当前Progress完成所需时间,单位毫秒
+ ///
+ public (long ElapsedTime, long NeedTime) GetProgress();
+ }
+
+ public class LongProgressByTime: ILongProgressByTime
+ {
+ // 根据时间推算Start后完成多少进度的进度条(long)。
+
+ // 只允许修改LongProgressByTime类中的代码
+ // 要求实现ILongProgressByTime中的要求
+ // 可利用Environment.TickCount64获取当前时间(单位ms)
+
+ long startTime;
+ long requiredTime;
+ long finishedTime;
+ bool isAtReady = true;
+
+ public bool Start(long needTime) {
+ object lockobject = new object();
+ lock (lockobject) {
+ if (isAtReady) {
+ isAtReady = false;
+ requiredTime = needTime;
+ startTime = Environment.TickCount64;
+ finishedTime = 0;
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+
+ public bool TrySet0() {
+ object lockobject = new object();
+ lock (lockobject) {
+ if (!isAtReady) {
+ isAtReady = true;
+ finishedTime = 0;
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+
+ public void Set0() {
+ object lockobject = new object();
+ lock (lockobject) {
+ finishedTime = 0;
+ }
+ }
+
+ public (long ElapsedTime, long NeedTime) GetProgress() {
+ object lockobject = new object();
+ lock (lockobject) {
+ long nowTime = Environment.TickCount64;
+ finishedTime = nowTime - startTime;
+ return (finishedTime, requiredTime);
+ }
+ }
+
+ //挑战:利用原子操作
+ //long.MaxValue非常久
+ }
+
+/*输出示例(仅供参考):
+ * A Start: False
+B Start: True
+A TrySet0: True
+B Start: True Now: 14536562
+A Start: False Now: 14536578
+B Progress: (516, 1000) Now: 14537078
+A Progress: (516, 1000) Now: 14537078
+A TrySet0: False
+*/
}
\ No newline at end of file
diff --git a/hw1/store.txt b/hw1/store.txt
new file mode 100644
index 0000000..93a8e25
--- /dev/null
+++ b/hw1/store.txt
@@ -0,0 +1,139 @@
+using System;
+using System.Diagnostics;
+
+namespace Homework
+{
+ public class Program
+ {
+ public static void Main(string[] args)
+ {
+ ILongProgressByTime a = new LongProgressByTime();
+ new Thread
+ (
+ () =>
+ {
+ Console.WriteLine("A Start: "+(a.Start(2000)).ToString());
+ Thread.Sleep(1000);
+ Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
+ Thread.Sleep(500);
+ Console.WriteLine("A Start: "+(a.Start(1000)).ToString() +" Now: "+Environment.TickCount64);
+ Thread.Sleep(500);
+ Console.WriteLine("A Progress: "+(a.GetProgress()).ToString() +" Now: "+Environment.TickCount64);
+ Thread.Sleep(1003);
+ Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString());
+ }
+ ).Start();
+
+ new Thread
+ (
+ () =>
+ {
+ Console.WriteLine("B Start: "+(a.Start(2000)).ToString());
+ Thread.Sleep(1500);
+ Console.WriteLine("B Start: " +(a.Start(1000)).ToString() + " Now: " + Environment.TickCount64);
+ Thread.Sleep(500);
+ Console.WriteLine("B Progress: " +(a.GetProgress()).ToString() + " Now: " + Environment.TickCount64);
+ }
+ ).Start();
+ }
+ }
+
+ public interface ILongProgressByTime
+ {
+ ///
+ /// 尝试加载下一次进度条,needTime指再次加载进度条所需时间,单位毫秒
+ /// 如果之前进度条处于就绪态,则将进度开始下一次加载,返回true
+ /// 如果之前进度条不处于就绪态,返回false
+ ///
+ public bool Start(long needTime);
+
+ ///
+ /// 使未完成的进度条清零并终止变为就绪态,返回值代表是否成功终止
+ ///
+ public bool TrySet0();
+
+ ///
+ /// 使进度条强制清零并终止变为就绪态
+ ///
+ public void Set0();
+
+ ///
+ /// ElapsedTime指其中已过去的时间,NeedTime指当前Progress完成所需时间,单位毫秒
+ ///
+ public (long ElapsedTime, long NeedTime) GetProgress();
+ }
+
+ public class LongProgressByTime: ILongProgressByTime
+ {
+ // 根据时间推算Start后完成多少进度的进度条(long)。
+
+ // 只允许修改LongProgressByTime类中的代码
+ // 要求实现ILongProgressByTime中的要求
+ // 可利用Environment.TickCount64获取当前时间(单位ms)
+
+ long startTime;
+ long requiredTime;
+ long finishedTime;
+ bool isAtReady = true;
+
+ public bool Start(long needTime) {
+ object lockobject = new object();
+ lock (lockobject) {
+ if (isAtReady) {
+ isAtReady = false;
+ requiredTime = needTime;
+ startTime = Environment.TickCount64;
+ finishedTime = 0;
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+
+ public bool TrySet0() {
+ object lockobject = new object();
+ lock (lockobject) {
+ if (!isAtReady) {
+ isAtReady = true;
+ finishedTime = 0;
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+
+ public void Set0() {
+ object lockobject = new object();
+ lock (lockobject) {
+ finishedTime = 0;
+ }
+ }
+
+ public (long ElapsedTime, long NeedTime) GetProgress() {
+ object lockobject = new object();
+ lock (lockobject) {
+ long nowTime = Environment.TickCount64;
+ finishedTime = nowTime - startTime;
+ return (finishedTime, requiredTime);
+ }
+ }
+
+ //挑战:利用原子操作
+ //long.MaxValue非常久
+ }
+
+/*输出示例(仅供参考):
+ * A Start: False
+B Start: True
+A TrySet0: True
+B Start: True Now: 14536562
+A Start: False Now: 14536578
+B Progress: (516, 1000) Now: 14537078
+A Progress: (516, 1000) Now: 14537078
+A TrySet0: False
+*/
+}
\ No newline at end of file