从c#wpf ping服务器时出现崩溃和蓝屏(ntoskrnl.exe)

时间:2016-08-12 05:46:25

标签: c# .net wpf ping crash-dumps

我正在编写一个小工具来检查c#wpf中服务器的ping延迟。我使用Windows Forms做了类似的事情,发生的问题似乎是一样的。过了一段时间,我得到一个蓝屏,包括内存转储。问题是由ntoskrnl.exe引起的,我不明白为什么。我所做的就是每1000毫秒ping一些服务器。

这是我的c#代码:

using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Net.NetworkInformation;
using System.ComponentModel;

namespace NetworkChecker
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
    ObservableCollection<PingTarget> PingTargets = new ObservableCollection<PingTarget>();
    PingTargetHost pth = new PingTargetHost();
    public MainWindow()
    {
        pth.PingTargetList = new ObservableCollection<PingTarget>();
        InitializeComponent();
        PingTarget ptExample = new PingTarget { TargetName = "Google", TargetIP = "www.google.ch" };
        pth.PingTargetList.Add(ptExample);

        PingTargetListBox.ItemsSource = pth.PingTargetList;
    }

    private void AddNewTarget_Click(object sender, RoutedEventArgs e)
    {
        PingTarget newTarget = new PingTarget { TargetName = NewTargetNametb.Text, TargetIP = NewTargetIPtb.Text };
        pth.PingTargetList.Add(newTarget);
    }
}

public class PingTarget : INotifyPropertyChanged
{
    public PingTarget()
    {
        PingCollection = new ObservableCollection<KeyValuePair<DateTime, long>>();
        this.Active = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }

    private string _TargetName;
    public string TargetName
    {
        get { return _TargetName; }
        set { _TargetName = value; OnPropertyChanged("TargetName"); }
    }

    private string _TargetIP;
    public string TargetIP
    {
        get { return _TargetIP; }
        set { _TargetIP = value; OnPropertyChanged("TargetIP"); }
    }

    private ObservableCollection<KeyValuePair<DateTime, long>> _PingCollection;
    public ObservableCollection<KeyValuePair<DateTime, long>> PingCollection 
    {
        get { return _PingCollection; }
        set { _PingCollection = value; OnPropertyChanged("PingCollection"); } 
    }

    public long MaxPing
    {
        get
        {
            return PingCollection.Max(p => p.Value);
        }
    }

    public long MinPing
    {
        get
        {
            return PingCollection.Min(p => p.Value);
        }
    }

    public long AvgPing
    {
        get
        {
            return (long)PingCollection.Average(p => p.Value);
        }
    }

    private bool _Active;
    public bool Active
    {
        get { return _Active; }
        set { _Active = value; OnPropertyChanged("Active"); }
    }
}

public class PingTargetHost : INotifyPropertyChanged
{
    public PingTargetHost(int PingInterval = 1000)
    {
        PingFrequency = PingInterval;
        Ticker = new DispatcherTimer();
        Ticker.Interval = new TimeSpan(0, 0, 0, 0, PingFrequency);
        Ticker.Tick += Ticker_Tick;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

    }

    void Ticker_Tick(object sender, EventArgs e)
    {
        foreach (PingTarget pt in PingTargetList.Where(pts => pts.Active == true))
        {
            Ping pingSender = new Ping();
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            int timeout = 10000;
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            PingReply Result = pingSender.Send(pt.TargetIP, timeout, buffer);
            string Pong = Result.RoundtripTime.ToString();
            if (Result.Status == IPStatus.Success)
            {
                KeyValuePair<DateTime, long> ping = new KeyValuePair<DateTime, long>(DateTime.Now, Result.RoundtripTime);
                pt.PingCollection.Add(ping);
            }
            if (Result.Status == IPStatus.TimedOut)
            {

            }
        }
    }

    public void StopTicker()
    {
        Ticker.Stop();
    }

    public void StartTicker()
    {
        Ticker.Start();
    }

    private DispatcherTimer Ticker { get; set; }
    public int PingFrequency { get; set; }
    public ObservableCollection<PingTarget> PingTargetList { get; set; }

}
}

这是我的WPF代码:

<Controls:MetroWindow x:Class="NetworkChecker.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="Simple Network Checker" Height="600" Width="1000">
<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.5*" />
        <ColumnDefinition Width="5" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <StackPanel Orientation="Horizontal"  Grid.Column="1" >
        <Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
    </StackPanel>
    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="5" />
            <RowDefinition Height="150" />
        </Grid.RowDefinitions>
        <ListBox x:Name="PingTargetListBox" DataContext="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Content="{Binding TargetName}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Separator Margin="2" Grid.Row="1" />
        <Grid Grid.Row="2">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBox x:Name="NewTargetIPtb" Controls:TextBoxHelper.Watermark="Target IP" Controls:TextBoxHelper.UseFloatingWatermark="True" />
            <TextBox x:Name="NewTargetNametb" Grid.Row="1" Controls:TextBoxHelper.Watermark="Target Name" Controls:TextBoxHelper.UseFloatingWatermark="True" />
            <Button x:Name="AddNewTarget" Click="AddNewTarget_Click" Content="Hinzufügen" Grid.Row="2" />
        </Grid>
    </Grid>
    <Grid Grid.Column="2" DataContext="{Binding ElementName=PingTargetListBox, Path=SelectedItem}">
        <Grid.RowDefinitions >
            <RowDefinition />
            <RowDefinition Height="5" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="DetailsPingListBox" ItemsSource="{Binding PingCollection}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Label Content="{Binding Key}" />
                        <Label Content="{Binding Value}" Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Separator Grid.Row="1" Margin="2" />
        <StackPanel Grid.Row="2">
            <CheckBox x:Name="ActiveInactive" IsChecked="{Binding Active}" Content="Aktiv" />
            <TextBlock>
                Maximum: <TextBlock Text="{Binding MaxPing}" />
            </TextBlock>
            <TextBlock>
                Minimum: <TextBlock Text="{Binding MinPing}" />
            </TextBlock>
            <TextBlock>
                Average: <TextBlock Text="{Binding AvgPing}" />
            </TextBlock>
        </StackPanel>
    </Grid>
</Grid>

有什么想法吗? 我真的很喜欢这个没有蓝屏的工具;)

非常感谢你的帮助!

0 个答案:

没有答案