使用Task
This commit is contained in:
		
							parent
							
								
									53e907d145
								
							
						
					
					
						commit
						f163e20d0a
					
				| @ -82,7 +82,7 @@ | ||||
|       <Generator>MSBuild:Compile</Generator> | ||||
|       <SubType>Designer</SubType> | ||||
|     </Page> | ||||
|     <Page Include="LogListBox.xaml"> | ||||
|     <Page Include="UdpLogListBox.xaml"> | ||||
|       <SubType>Designer</SubType> | ||||
|       <Generator>MSBuild:Compile</Generator> | ||||
|     </Page> | ||||
| @ -98,8 +98,8 @@ | ||||
|     <Compile Include="FlowChart.xaml.cs"> | ||||
|       <DependentUpon>FlowChart.xaml</DependentUpon> | ||||
|     </Compile> | ||||
|     <Compile Include="LogListBox.xaml.cs"> | ||||
|       <DependentUpon>LogListBox.xaml</DependentUpon> | ||||
|     <Compile Include="UdpLogListBox.xaml.cs"> | ||||
|       <DependentUpon>UdpLogListBox.xaml</DependentUpon> | ||||
|     </Compile> | ||||
|     <Compile Include="MainWindow.xaml.cs"> | ||||
|       <DependentUpon>MainWindow.xaml</DependentUpon> | ||||
|  | ||||
| @ -152,7 +152,7 @@ | ||||
| 
 | ||||
|             <TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Header="日志记录" Height="40" Width="100" Margin="5 0" FontSize="18"> | ||||
|                 <Grid Background="#f7f7f7"> | ||||
|                     <local:LogListBox Margin="16"></local:LogListBox> | ||||
|                     <local:UdpLogListBox/> | ||||
|                 </Grid> | ||||
|             </TabItem> | ||||
| 
 | ||||
|  | ||||
| @ -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:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||||
|              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  | ||||
| @ -15,16 +15,26 @@ | ||||
|             ScrollViewer.HorizontalScrollBarVisibility="Disabled"  | ||||
|             BorderThickness="0" | ||||
|             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> | ||||
|                 <DataTemplate> | ||||
|                     <StackPanel Margin="0 5"> | ||||
|                     <StackPanel Margin="16 5"> | ||||
|                         <StackPanel.ContextMenu> | ||||
|                             <ContextMenu> | ||||
|                                 <MenuItem Header="复制本项" Click="MenuItem_Copy_Click" /> | ||||
|                                 <MenuItem Header="清除所有" Click="MenuItem_Clear_Click" /> | ||||
|                             </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="14" FontWeight="Light" Text="{Binding Message}"> | ||||
|                             <TextBlock.Foreground> | ||||
| @ -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 | ||||
| { | ||||
|     /// <summary> | ||||
|     /// LogListBox.xaml 的交互逻辑 | ||||
|     /// UdpLogListBox.xaml 的交互逻辑 | ||||
|     /// </summary> | ||||
|     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<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>(); | ||||
| 
 | ||||
|         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<UdpLog> 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<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) | ||||
|         { | ||||
|             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<UdpLog>(json); | ||||
|             this.Dispatcher.Invoke(() => this.LogList.Add(log)); | ||||
|             this.BeginReceiveFrom(); | ||||
|             var taskCompletionSource = (TaskCompletionSource<int>)ar.AsyncState; | ||||
|             taskCompletionSource.TrySetResult(length); | ||||
|         } | ||||
| 
 | ||||
|         private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user