1

I ping 50 IP addresses but the program freezes when I click start.

How can i do it without freezing.

private void button1_Click(object sender, EventArgs e)
{
    string IP = textBox1.Text;
    string[] IPBlocks = IP.Split('.');
    for (int x = 0; x < 50; x++)
    {
        System.Threading.Thread.Sleep(50);
        int IPLastBlock = Int32.Parse(IPBlocks[3]) + (x+1);
        IP = IPBlocks[0]+"."+ IPBlocks[1]+"."+ IPBlocks[2]+"."+ IPLastBlock;
        bool pingStatus = PingHost(IP);
        textBox2.Text += String.Format("{0} => {1} \r\n", IP, pingStatus);
    }
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
tyasird
  • 926
  • 1
  • 12
  • 29
  • Put the ping process (for loop) on separate thread and show progress bar until thread process is completed. – Kamalesh Wankhede Nov 29 '16 at 05:51
  • 1
    You cannot do lengthy tasks in your event handler, that'd block the UI thread and stop the UI from updating. You need to create a new thread to do your processing. See http://stackoverflow.com/questions/28194943/creating-and-starting-a-task-on-the-ui-thread and http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – KC Wong Nov 29 '16 at 05:51

2 Answers2

3

On button click launch a separte thread, this will not hang your screen

    private void button1_Click(object sender, EventArgs e)
    {
        Thread newThread= new Thread(PingIP);
        newThread.Start(); 
    }

    private void PingIP()
    {
        string IP = textBox1.Text;
        string[] IPBlocks = IP.Split('.');
        for (int x = 0; x < 50; x++)
        {
            System.Threading.Thread.Sleep(50);
            int IPLastBlock = Int32.Parse(IPBlocks[3]) + (x+1);
            IP = IPBlocks[0]+"."+ IPBlocks[1]+"."+ IPBlocks[2]+"."+ IPLastBlock;
            bool pingStatus = PingHost(IP);
            textBox2.Text += String.Format("{0} => {1} \r\n", IP, pingStatus);
        }
    }
farrukh aziz
  • 162
  • 1
  • 2
  • 9
  • 1
    error returns in textBox2.Text Line "Cross-thread operation is not valid: The 'textBox2' control was accessed from another thread other than the thread that was built." – tyasird Nov 29 '16 at 06:38
  • Use `Invoke` statement to set `textBox2.Text` to get rid of Cross thread exception. It will execute `textBox2.Text = someString;` on Main thread. – Kamalesh Wankhede Nov 29 '16 at 06:45
  • 1
    Something like: `Invoke(new MethodInvoker(() => { textBox2.Text += String.Format("{0} => {1} \r\n", IP, pingStatus); }));` – Kamalesh Wankhede Nov 29 '16 at 06:51
1

Use ThreadPool.QueueUserWorkItem

ThreadPool.QueueUserWorkItem(delegate {
    bool pingStatus = PingHost(IP);
    textBox2.Text += String.Format("{0} => {1} \r\n", IP, pingStatus);
});
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • 2
    Is it a good thing to access `textBox2.Text` from another thread? – Kamalesh Wankhede Nov 29 '16 at 05:57
  • @krw12572 if you are going to do all the checkings before work with that field it's ok, but if not you can get exceptions like null value and etc. – Samvel Petrosov Nov 29 '16 at 06:04
  • thanks but error message returns "Cross-thread operation is not valid: The 'textBox2' control was accessed from another thread other than the thread that was built." – tyasird Nov 29 '16 at 06:30
  • @tyasird, That's the one I was talking about. Well, you could use `Invoke` statement to set `textBox2.Text` to get rid of Cross thread exception. It will execute `textBox2.Text = someString;` on Main thread. – Kamalesh Wankhede Nov 29 '16 at 06:36