使用Task

This commit is contained in:
老九 2021-11-02 22:51:54 +08:00
parent 53e907d145
commit f163e20d0a
4 changed files with 43 additions and 19 deletions

View File

@ -82,7 +82,7 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
<Page Include="LogListBox.xaml"> <Page Include="UdpLogListBox.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
@ -98,8 +98,8 @@
<Compile Include="FlowChart.xaml.cs"> <Compile Include="FlowChart.xaml.cs">
<DependentUpon>FlowChart.xaml</DependentUpon> <DependentUpon>FlowChart.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="LogListBox.xaml.cs"> <Compile Include="UdpLogListBox.xaml.cs">
<DependentUpon>LogListBox.xaml</DependentUpon> <DependentUpon>UdpLogListBox.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="MainWindow.xaml.cs"> <Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon> <DependentUpon>MainWindow.xaml</DependentUpon>

View File

@ -152,7 +152,7 @@
<TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Header="日志记录" Height="40" Width="100" Margin="5 0" FontSize="18"> <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Header="日志记录" Height="40" Width="100" Margin="5 0" FontSize="18">
<Grid Background="#f7f7f7"> <Grid Background="#f7f7f7">
<local:LogListBox Margin="16"></local:LogListBox> <local:UdpLogListBox/>
</Grid> </Grid>
</TabItem> </TabItem>

View File

@ -1,4 +1,4 @@
<UserControl x:Class="FastGithub.UI.LogListBox" <UserControl x:Class="FastGithub.UI.UdpLogListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@ -15,16 +15,26 @@
ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
BorderThickness="0" BorderThickness="0"
Background="#f7f7f7"> Background="#f7f7f7">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Margin="0 5"> <StackPanel Margin="16 5">
<StackPanel.ContextMenu> <StackPanel.ContextMenu>
<ContextMenu> <ContextMenu>
<MenuItem Header="复制本项" Click="MenuItem_Copy_Click" /> <MenuItem Header="复制本项" Click="MenuItem_Copy_Click" />
<MenuItem Header="清除所有" Click="MenuItem_Clear_Click" /> <MenuItem Header="清除所有" Click="MenuItem_Clear_Click" />
</ContextMenu> </ContextMenu>
</StackPanel.ContextMenu> </StackPanel.ContextMenu>
<TextBlock TextWrapping="Wrap" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/> <TextBlock TextWrapping="Wrap" FontSize="12" Text="{Binding Timestamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 5" Text="{Binding SourceContext}"/> <TextBlock TextWrapping="Wrap" FontSize="12" Margin="0 5" Text="{Binding SourceContext}"/>
<TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Text="{Binding Message}"> <TextBlock TextWrapping="Wrap" FontSize="14" FontWeight="Light" Text="{Binding Message}">
<TextBlock.Foreground> <TextBlock.Foreground>

View File

@ -3,43 +3,57 @@ using System.Collections.ObjectModel;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
namespace FastGithub.UI namespace FastGithub.UI
{ {
/// <summary> /// <summary>
/// LogListBox.xaml 的交互逻辑 /// UdpLogListBox.xaml 的交互逻辑
/// </summary> /// </summary>
public partial class LogListBox : UserControl public partial class UdpLogListBox : UserControl
{ {
private readonly byte[] buffer = new byte[ushort.MaxValue]; private readonly byte[] buffer = new byte[ushort.MaxValue];
private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp); private readonly Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>(); public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>();
public LogListBox() public UdpLogListBox()
{ {
InitializeComponent(); InitializeComponent();
DataContext = this; DataContext = this;
this.socket.Bind(new IPEndPoint(IPAddress.Loopback, UdpLoggerPort.Value)); this.InitUdpLoggerAsync();
this.BeginReceiveFrom();
} }
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<UdpLog> GetUdpLogAsync()
{ {
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); 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<int>();
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<UdpLog>(json);
} }
private void EndReceiveFrom(IAsyncResult ar) private void EndReceiveFrom(IAsyncResult ar)
{ {
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
var length = this.socket.EndReceiveFrom(ar, ref remoteEP); var length = this.socket.EndReceiveFrom(ar, ref remoteEP);
var json = Encoding.UTF8.GetString(buffer, 0, length); var taskCompletionSource = (TaskCompletionSource<int>)ar.AsyncState;
var log = Newtonsoft.Json.JsonConvert.DeserializeObject<UdpLog>(json); taskCompletionSource.TrySetResult(length);
this.Dispatcher.Invoke(() => this.LogList.Add(log));
this.BeginReceiveFrom();
} }
private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e) private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)