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)