From f163e20d0aa57c47d1465228f543dba1ea4da8b0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Tue, 2 Nov 2021 22:51:54 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Task?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.UI/FastGithub.UI.csproj | 6 ++--
FastGithub.UI/MainWindow.xaml | 2 +-
.../{LogListBox.xaml => UdpLogListBox.xaml} | 18 +++++++---
...gListBox.xaml.cs => UdpLogListBox.xaml.cs} | 36 +++++++++++++------
4 files changed, 43 insertions(+), 19 deletions(-)
rename FastGithub.UI/{LogListBox.xaml => UdpLogListBox.xaml} (68%)
rename FastGithub.UI/{LogListBox.xaml.cs => UdpLogListBox.xaml.cs} (62%)
diff --git a/FastGithub.UI/FastGithub.UI.csproj b/FastGithub.UI/FastGithub.UI.csproj
index fec018e..0908950 100644
--- a/FastGithub.UI/FastGithub.UI.csproj
+++ b/FastGithub.UI/FastGithub.UI.csproj
@@ -82,7 +82,7 @@
MSBuild:Compile
Designer
-
+
Designer
MSBuild:Compile
@@ -98,8 +98,8 @@
FlowChart.xaml
-
- LogListBox.xaml
+
+ UdpLogListBox.xaml
MainWindow.xaml
diff --git a/FastGithub.UI/MainWindow.xaml b/FastGithub.UI/MainWindow.xaml
index 97b193a..97b6371 100644
--- a/FastGithub.UI/MainWindow.xaml
+++ b/FastGithub.UI/MainWindow.xaml
@@ -152,7 +152,7 @@
-
+
diff --git a/FastGithub.UI/LogListBox.xaml b/FastGithub.UI/UdpLogListBox.xaml
similarity index 68%
rename from FastGithub.UI/LogListBox.xaml
rename to FastGithub.UI/UdpLogListBox.xaml
index 9a241c7..cb459ed 100644
--- a/FastGithub.UI/LogListBox.xaml
+++ b/FastGithub.UI/UdpLogListBox.xaml
@@ -1,4 +1,4 @@
-
+
+
+
-
+
-
+
diff --git a/FastGithub.UI/LogListBox.xaml.cs b/FastGithub.UI/UdpLogListBox.xaml.cs
similarity index 62%
rename from FastGithub.UI/LogListBox.xaml.cs
rename to FastGithub.UI/UdpLogListBox.xaml.cs
index 8835fb8..be1421b 100644
--- a/FastGithub.UI/LogListBox.xaml.cs
+++ b/FastGithub.UI/UdpLogListBox.xaml.cs
@@ -3,43 +3,57 @@ using System.Collections.ObjectModel;
using System.Net;
using System.Net.Sockets;
using System.Text;
+using System.Threading.Tasks;
using System.Windows.Controls;
namespace FastGithub.UI
{
///
- /// LogListBox.xaml 的交互逻辑
+ /// UdpLogListBox.xaml 的交互逻辑
///
- public partial class LogListBox : UserControl
+ public partial class UdpLogListBox : UserControl
{
private readonly byte[] buffer = new byte[ushort.MaxValue];
private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
public ObservableCollection LogList { get; } = new ObservableCollection();
- public LogListBox()
+ public UdpLogListBox()
{
InitializeComponent();
DataContext = this;
- this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
- this.BeginReceiveFrom();
+ this.InitUdpLoggerAsync();
}
- private void BeginReceiveFrom()
+ private async void InitUdpLoggerAsync()
+ {
+ this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value));
+ while (true)
+ {
+ var log = await this.GetUdpLogAsync();
+ this.LogList.Add(log);
+ }
+ }
+
+ private async Task GetUdpLogAsync()
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
- this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, null);
+ var taskCompletionSource = new TaskCompletionSource();
+ this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, this.EndReceiveFrom, taskCompletionSource);
+ var length = await taskCompletionSource.Task;
+
+ var json = Encoding.UTF8.GetString(buffer, 0, length);
+ return Newtonsoft.Json.JsonConvert.DeserializeObject(json);
}
+
private void EndReceiveFrom(IAsyncResult ar)
{
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var length = this.socket.EndReceiveFrom(ar, ref remoteEP);
- var json = Encoding.UTF8.GetString(buffer, 0, length);
- var log = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
- this.Dispatcher.Invoke(() => this.LogList.Add(log));
- this.BeginReceiveFrom();
+ var taskCompletionSource = (TaskCompletionSource)ar.AsyncState;
+ taskCompletionSource.TrySetResult(length);
}
private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)