我想做什么: 我希望我的程序ping服务器以查看它是否已启动。 另外我想要选项勾选一个复选框以进行持久ping。这是Ping每30秒(9999次) 如果我输入的IP地址没有复选框并按回车键,则可以正常工作。 如果我勾选复选框,30秒后,我得到一个成功的ping(假设ping成功),但它从未尝试过第二次。 我认为问题在于for循环中的计时器。 简单来说,我想: - 如果勾选了复选框 然后每30秒进行9999次 - 运行ping方法 - 运行一次 我已经阅读了很多关于此的内容,但是,我已经达到了理解的边缘。我们将非常感激地提供任何帮助/提示!
这是我的代码(我认为计算的位数)
using System.Net.NetworkInformation;
using System.Timers;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace PingProject
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
textBoxIPaddress1.KeyDown += textBoxIPaddress1_KeyDown;
}
private void textBoxIPaddress1_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Enter))
{
if (every30secs1.IsChecked.Equals(true))
{
for (int pingCount = 0; pingCount < 9999; pingCount++)
{
pingAction1(this, new RoutedEventArgs());
timer = new Timer(30000);
timer.Enabled = true;
timer.Start();
timer.Elapsed += Timer_Elapsed;
}
}
else pingAction1(this, new RoutedEventArgs());
}
}
public static Timer timer;
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
timer.Dispose();
}
public void pingAction1(object sender, RoutedEventArgs e)
{
try
{
Ping Ping1 = new Ping();
PingReply pingReply1 = Ping1.Send(textBoxIPaddress1.Text, 1000);
textBoxResponseTime1.Text = (pingReply1.RoundtripTime.ToString() + "ms");
if (pingReply1.Status == IPStatus.Success)
{
successIndicatorRectangle1.Fill = new SolidColorBrush(Color.FromRgb(0, 111, 0));
textBoxError1.Text = ("Successful");
}
else
{
successIndicatorRectangle1.Fill = new SolidColorBrush(Color.FromRgb(222, 0, 0));
textBoxError1.Text = ("Not successful");
}
}
catch
{
textBoxError1.Text = ("Ping operation failed, check IP address or domain name");
successIndicatorRectangle1.Fill = new SolidColorBrush(Color.FromRgb(222, 0, 0));
}
}
private void buttonClear1_Click(object sender, RoutedEventArgs e)
{
successIndicatorRectangle1.Fill = new SolidColorBrush(Color.FromRgb(102, 102, 102));
textBoxError1.Text = (null);
textBoxIPaddress1.Text = (null);
textBoxResponseTime1.Text = (null);
}
}
}
XAML是:
<Window x:Class="PingProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PingProject"
mc:Ignorable="d"
Title="MainWindow" Height="431" Width="753">
<Grid x:Name="um" Margin="19,35,0,10" HorizontalAlignment="Left" Width="714" RenderTransformOrigin="0.413,0.534">
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="117*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*"/>
<ColumnDefinition Width="19*"/>
<ColumnDefinition Width="296*"/>
<ColumnDefinition Width="65*"/>
<ColumnDefinition Width="260*"/>
<ColumnDefinition Width="74*"/>
</Grid.ColumnDefinitions>
<Rectangle x:Name="successIndicatorRectangle1" Grid.Column="2" Fill="#666666" HorizontalAlignment="Left" Height="45" Margin="0,52,0,0" Grid.Row="1" Stroke="Black" VerticalAlignment="Top" Width="611" Grid.ColumnSpan="3"/>
<Label x:Name="labelPing_Project" Content="Ping Project" HorizontalAlignment="Left" Margin="256,9,0,0" VerticalAlignment="Top" Width="95" Height="35" Grid.Column="2" FontSize="18" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
<TextBox x:Name="textBoxIPaddress1" HorizontalAlignment="Left" Height="23" Margin="10,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" SelectionOpacity="0" Grid.Column="2" Grid.Row="1" ToolTip="Enter IP address" Cursor="IBeam" IsReadOnlyCaretVisible="True" ForceCursor="True"/>
<TextBox x:Name="textBoxResponseTime1" HorizontalAlignment="Left" Height="23" Margin="216,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="55" SelectionOpacity="0" Grid.Column="2" Grid.Row="1" ToolTip="Response Time" RenderTransformOrigin="-1.34,2.652"/>
<TextBox x:Name="textBoxError1" Grid.Column="3" Height="23" Margin="0,64,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Results" VerticalAlignment="Top" Cursor="No" Grid.ColumnSpan="2" HorizontalAlignment="Left" Width="305"/>
<CheckBox x:Name="every30secs1" Content="" Grid.Column="2" HorizontalAlignment="Left" Margin="181,64,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="0.588,0.812" Width="20" Height="23" ToolTip="Tick this box to ping every 30 secs"/>
<Label x:Name="labelHost1" Content="Host" Grid.Column="2" HorizontalAlignment="Left" Margin="56,26,0,0" Grid.Row="1" VerticalAlignment="Top" Height="26" Width="35"/>
<Label x:Name="label1" Content="" Grid.Column="2" HorizontalAlignment="Left" Margin="150,20,0,0" Grid.Row="1" VerticalAlignment="Top" Height="26" Width="10"/>
<Label x:Name="labelResponse1" Content="Response" Grid.Column="2" HorizontalAlignment="Left" Margin="216,26,0,0" Grid.Row="1" VerticalAlignment="Top" Height="26" Width="61"/>
<Label x:Name="labelNotes1" Content="Notes" Grid.Column="4" HorizontalAlignment="Left" Margin="35,26,0,0" Grid.Row="1" VerticalAlignment="Top" Height="26" Width="41"/>
<Button x:Name="buttonClear1" Content="Clear" Grid.Column="5" HorizontalAlignment="Left" Margin="0,52,0,0" Grid.Row="1" VerticalAlignment="Top" Width="40" Height="45" IsCancel="True" Click="buttonClear1_Click"/>
</Grid>
</Window>
这是我在这里的第一篇文章,所以如果我搞砸了,抱歉,请善待并指导我= 0)
答案 0 :(得分:0)
你必须改变你正在做的事情。恕我直言,流程应该是这样的:
在every30secs1.CheckedChanged
处理程序上,您必须检查。当复选框转到Checked = true时,您必须使用以下代码激活计时器:
timer.Start();
每当Checked属性更改为false时,则:
timer.Stop();
然后,在你Timer_Elapsed
的处理程序上,你必须这样做:
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
timer.Stop();
for(int i = 0; i < 9999 ; i++)
{
pingAction1(this, new RoutedEventArgs());
counter ++;
Thread.Sleep(30000);
}
}
catch (Exception ex)
{
//Do something
}
finally
{
timer.Start();
}
}