CancellationToken Test

main
syneffort 2 years ago
parent 4b4d64331e
commit e10ff8bade
  1. 10
      PacticeSolution/CancellationTokenTest/CancellationTokenTest.csproj
  2. 39
      PacticeSolution/CancellationTokenTest/Client.cs
  3. 25
      PacticeSolution/CancellationTokenTest/Program.cs
  4. 6
      PacticeSolution/PacticeSolution.sln
  5. 21
      PacticeSolution/TelnetCommunicator/MainWindow.xaml.cs

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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();
}
}
}

@ -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);
}
}
}
}

@ -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

@ -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);

Loading…
Cancel
Save