From e10ff8bade96c484ec54287e219cb116acc38870 Mon Sep 17 00:00:00 2001 From: syneffort Date: Wed, 20 Dec 2023 16:20:32 +0900 Subject: [PATCH] CancellationToken Test --- .../CancellationTokenTest.csproj | 10 +++++ .../CancellationTokenTest/Client.cs | 39 +++++++++++++++++++ .../CancellationTokenTest/Program.cs | 25 ++++++++++++ PacticeSolution/PacticeSolution.sln | 6 +++ .../TelnetCommunicator/MainWindow.xaml.cs | 21 +++++++++- 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 PacticeSolution/CancellationTokenTest/CancellationTokenTest.csproj create mode 100644 PacticeSolution/CancellationTokenTest/Client.cs create mode 100644 PacticeSolution/CancellationTokenTest/Program.cs diff --git a/PacticeSolution/CancellationTokenTest/CancellationTokenTest.csproj b/PacticeSolution/CancellationTokenTest/CancellationTokenTest.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/PacticeSolution/CancellationTokenTest/CancellationTokenTest.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/PacticeSolution/CancellationTokenTest/Client.cs b/PacticeSolution/CancellationTokenTest/Client.cs new file mode 100644 index 0000000..b1e0457 --- /dev/null +++ b/PacticeSolution/CancellationTokenTest/Client.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CancellationTokenTest +{ + public class Client + { + private CancellationTokenSource _cts; + private uint _cnt; + + public void Start() + { + Console.WriteLine("[Call Start]"); + _cts = new CancellationTokenSource(); + + Task.Run(() => + { + while (true) + { + Thread.Sleep(30); + + if (_cts.IsCancellationRequested) + break; + Console.WriteLine(_cnt++); + //Thread.Sleep(100); + } + }, _cts.Token); + } + + public void Cancel() + { + Console.WriteLine("[Call Cancel]"); + _cts.Cancel(); + } + } +} diff --git a/PacticeSolution/CancellationTokenTest/Program.cs b/PacticeSolution/CancellationTokenTest/Program.cs new file mode 100644 index 0000000..5d2ecfb --- /dev/null +++ b/PacticeSolution/CancellationTokenTest/Program.cs @@ -0,0 +1,25 @@ +namespace CancellationTokenTest +{ + internal class Program + { + static void Main(string[] args) + { + Client client = new Client(); + + for (int i = 0; i < 10; i++) + { + if (i % 2 == 0) + { + client.Start(); + } + else + { + client.Cancel(); + } + + //await Task.Delay(1000); + Thread.Sleep(1000); + } + } + } +} \ No newline at end of file diff --git a/PacticeSolution/PacticeSolution.sln b/PacticeSolution/PacticeSolution.sln index b7716d9..8347091 100644 --- a/PacticeSolution/PacticeSolution.sln +++ b/PacticeSolution/PacticeSolution.sln @@ -149,6 +149,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlurText", "BlurText\BlurTe EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelnetCommunicator", "TelnetCommunicator\TelnetCommunicator.csproj", "{06C7726F-CD7F-409A-97ED-343FFCD13BAA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CancellationTokenTest", "CancellationTokenTest\CancellationTokenTest.csproj", "{BB6710B9-7E6B-4251-ACCA-7640A701CD57}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -447,6 +449,10 @@ Global {06C7726F-CD7F-409A-97ED-343FFCD13BAA}.Debug|Any CPU.Build.0 = Debug|Any CPU {06C7726F-CD7F-409A-97ED-343FFCD13BAA}.Release|Any CPU.ActiveCfg = Release|Any CPU {06C7726F-CD7F-409A-97ED-343FFCD13BAA}.Release|Any CPU.Build.0 = Release|Any CPU + {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB6710B9-7E6B-4251-ACCA-7640A701CD57}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PacticeSolution/TelnetCommunicator/MainWindow.xaml.cs b/PacticeSolution/TelnetCommunicator/MainWindow.xaml.cs index 3f77452..3de7e42 100644 --- a/PacticeSolution/TelnetCommunicator/MainWindow.xaml.cs +++ b/PacticeSolution/TelnetCommunicator/MainWindow.xaml.cs @@ -33,14 +33,21 @@ namespace TelnetCommunicator private void InitInstance() { _tap = new TAP("200.200.200.123", 23); + _tap.ErrorHandler -= On_Received; _tap.ErrorHandler += On_Received; } private async void btnStatus_Click(object sender, RoutedEventArgs e) { string response = await _tap.SendMessage("STA"); - tbMessage.AppendText(GetInfo(response) + Environment.NewLine); + //tbMessage.AppendText(GetInfo(response) + Environment.NewLine); + tbMessage.AppendText(response + Environment.NewLine); tbMessage.ScrollToEnd(); + + if (CheckConnectionError(response)) + { + InitInstance(); + } } private void On_Received(object sender, Exception e) @@ -52,6 +59,18 @@ namespace TelnetCommunicator })); } + private bool CheckConnectionError(string message) + { + string[] lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + foreach (string line in lines) + { + if (line.IndexOf("trouble") > 0) + return true; + } + + return false; + } + private string GetInfo(string message) { string[] lines = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);