parent
f3fdfbfa1f
commit
df342e2304
7 changed files with 232 additions and 0 deletions
@ -0,0 +1,114 @@ |
||||
using log4net; |
||||
using SparkClient.Views.Dialog; |
||||
|
||||
namespace SparkClient.Model.Helper; |
||||
using System.Management; |
||||
public class NetworkSpeedHelper |
||||
{ |
||||
|
||||
private static readonly ILog Logger = LogManager.GetLogger(typeof(NetworkSpeedHelper)); |
||||
private static int _cycle = 300000; |
||||
|
||||
private static bool _ignore = false; |
||||
public static bool Ignore { |
||||
get => _ignore; |
||||
set |
||||
{ |
||||
if (_ignore) |
||||
{ |
||||
DisposeTimer(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static Timer _monitorTimer; |
||||
private static object _lock = new object(); |
||||
|
||||
public static void StartMonitoring() |
||||
{ |
||||
Logger.Info($"周期网络检测开始,周期: {_cycle}"); |
||||
lock (_lock) |
||||
{ |
||||
if (_monitorTimer == null) |
||||
{ |
||||
_monitorTimer = new Timer(CheckSpeedCallback, |
||||
null, |
||||
_cycle, |
||||
Timeout.Infinite); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static void CheckSpeedCallback(object state) |
||||
{ |
||||
try |
||||
{ |
||||
// 检查停止标志 |
||||
if (Ignore) |
||||
{ |
||||
DisposeTimer(); |
||||
return; |
||||
} |
||||
|
||||
// 执行网络测速 |
||||
var speed = InternetSpeedDetection(); |
||||
|
||||
// 触发阈值报警 |
||||
if (speed < 866) |
||||
{ |
||||
ShowSpeedAlert(speed); |
||||
} |
||||
|
||||
// 重置定时器(单次触发模式) |
||||
lock (_lock) |
||||
{ |
||||
if (!Ignore && _monitorTimer != null) |
||||
{ |
||||
_monitorTimer.Change(_cycle, Timeout.Infinite); |
||||
} |
||||
} |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
Logger.Info($"监控异常: {ex.Message}"); |
||||
} |
||||
} |
||||
|
||||
private static void DisposeTimer() |
||||
{ |
||||
lock (_lock) |
||||
{ |
||||
Logger.Info("网络检测取消!"); |
||||
_monitorTimer?.Dispose(); |
||||
_monitorTimer = null; |
||||
} |
||||
} |
||||
|
||||
private static void ShowSpeedAlert(double speed) |
||||
{ |
||||
var result = MessageBoxHasCheck.ShowMessageDialog() ; |
||||
NetworkSpeedHelper.Ignore = result; |
||||
} |
||||
|
||||
private static double InternetSpeedDetection() |
||||
{ |
||||
try { |
||||
// 连接到WMI命名空间 |
||||
var searcher = new ManagementObjectSearcher( |
||||
"SELECT Name, Speed FROM Win32_NetworkAdapter " + |
||||
"WHERE NetConnectionStatus = 2"); |
||||
foreach (ManagementObject obj in searcher.Get()) { |
||||
string name = obj["Name"].ToString(); |
||||
if (obj["Speed"] != null) { |
||||
long speedBps = Convert.ToInt64(obj["Speed"]); |
||||
double speedMbps = speedBps / 1_000_000.0; |
||||
return speedMbps; |
||||
} |
||||
} |
||||
return 0; |
||||
} catch (Exception ex) |
||||
{ |
||||
return 0; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@ |
||||
<Window x:Class="SparkClient.Views.Dialog.MessageBoxHasCheck" |
||||
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" |
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
xmlns:local="clr-namespace:SparkClient.Views.Dialog" |
||||
mc:Ignorable="d" |
||||
WindowStyle="None" |
||||
AllowsTransparency="True" |
||||
Background="Transparent" |
||||
ResizeMode="NoResize" |
||||
Title="MessageBox" Height="Auto" Width="600"> |
||||
<Window.Resources> |
||||
<Style x:Key="CloseHoverImageStyle" TargetType="Image"> |
||||
<Setter Property="Source" Value="pack://application:,,,/Resource/Images/UIResource/01-8.png"/> |
||||
<Style.Triggers> |
||||
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Border}}" Value="True"> |
||||
<Setter Property="Source" Value="pack://application:,,,/Resource/Images/UIResource/01-8-1.png"/> |
||||
</DataTrigger> |
||||
</Style.Triggers> |
||||
</Style> |
||||
</Window.Resources> |
||||
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown"> |
||||
<Border CornerRadius="20" |
||||
BorderThickness="1" |
||||
BorderBrush="LightGray" |
||||
Background="White"> |
||||
<Grid> |
||||
<Grid.RowDefinitions> |
||||
<RowDefinition Height="50"/> |
||||
<RowDefinition Height="Auto"/> |
||||
<RowDefinition Height="Auto"/> |
||||
<RowDefinition Height="Auto"/> |
||||
</Grid.RowDefinitions> |
||||
|
||||
<TextBlock Grid.Row="1" Width="600" TextWrapping = "Wrap" Padding="30 0" HorizontalAlignment="Center" |
||||
Text="{StaticResource NetworkSpeedCheckedMessage}" |
||||
VerticalAlignment="Center" TextAlignment="Center" FontSize="18" x:Name="TextBlockCenterBox" Margin="0 20" |
||||
/> |
||||
<CheckBox Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{StaticResource NetworkSpeedCheckedText}" |
||||
FontSize="16" x:Name="CheckBoxCancelCheck"/> |
||||
|
||||
<Grid Grid.Row="3" x:Name="GridButtons" Margin="0 20"> |
||||
<Grid.ColumnDefinitions> |
||||
<ColumnDefinition Width="*" /> |
||||
<ColumnDefinition Width="Auto" /> |
||||
<ColumnDefinition Width="*" /> |
||||
</Grid.ColumnDefinitions> |
||||
|
||||
<Button x:Name="ConfirmButton" Grid.Column="1" Padding="0" Margin="0 0 0 20" Height="50" Click="ConfirmButton_OnClick" |
||||
BorderThickness="0" Background="Transparent" FocusVisualStyle="{x:Null}"> |
||||
<Border |
||||
HorizontalAlignment="Center" |
||||
VerticalAlignment="Center" |
||||
Margin="0 0 0 0" |
||||
Padding="10, 10" |
||||
Width="120" |
||||
BorderThickness="0" |
||||
CornerRadius="20"> |
||||
<Border.Background> |
||||
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> |
||||
<GradientStop Color="#40b1ff" Offset="0"/> |
||||
<GradientStop Color="#3c9ce6" Offset="1"/> |
||||
</LinearGradientBrush> |
||||
</Border.Background> |
||||
<TextBlock FontSize="18" Text="{StaticResource Confirm}" TextAlignment="Center" Foreground="#ffffff"></TextBlock> |
||||
</Border> |
||||
</Button> |
||||
|
||||
</Grid> |
||||
</Grid> |
||||
</Border> |
||||
</Grid> |
||||
</Window> |
||||
@ -0,0 +1,35 @@ |
||||
using System.Windows; |
||||
using System.Windows.Input; |
||||
|
||||
namespace SparkClient.Views.Dialog; |
||||
|
||||
public partial class MessageBoxHasCheck : Window |
||||
{ |
||||
public MessageBoxHasCheck() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
|
||||
|
||||
|
||||
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
||||
{ |
||||
if (e.ButtonState == MouseButtonState.Pressed) |
||||
{ |
||||
this.DragMove(); |
||||
} |
||||
} |
||||
|
||||
private void ConfirmButton_OnClick(object sender, RoutedEventArgs e) |
||||
{ |
||||
this.Close(); |
||||
} |
||||
|
||||
public static bool ShowMessageDialog() |
||||
{ |
||||
var dialog = new MessageBoxHasCheck(); |
||||
dialog.ShowDialog(); |
||||
var data = dialog.CheckBoxCancelCheck.IsChecked == true; |
||||
return data; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue