mercredi 4 janvier 2017

c# Timer not working as expected, see example below

What I am trying to do: I want my program to ping a server to see if it is up. Additionally I want to want the option to tick a checkbox for persistent pinging. That is Ping every 30 seconds (for 9999 times) If I enter an IP address without the checkbox and press enter, it works. If I tick the checkbox, after 30 secs, I get a successful ping (assuming the ping was successful)but it never tries a second time. I think the problem is with the timer inside the for loop. In easy terms I want: -If checkbox is ticked -then every 30 seconds for 9999 times -run the ping method -else run it once I have read a lot about this but tbh, Ive reached the edge of my understanding. Any help/tips will be gratefully received!

Here is my code (the bits I think count)

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 is:

<Window x:Class="PingProject.MainWindow"
        xmlns="http://ift.tt/o66D3f"
        xmlns:x="http://ift.tt/mPTqtT"
        xmlns:d="http://ift.tt/pHvyf2"
        xmlns:mc="http://ift.tt/pzd6Lm"
        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>

This is my first post here, so If I have messed it up, sorry, please be kind and guide me =0)




Aucun commentaire:

Enregistrer un commentaire