I'm creating a small port scanning app which use a port range defined by user.
My problem with the code is that I find it not efficient enough meaning that it ping very slow, especially when ports are not responding meaning that I must wait for the timeout before continuing to next port.
I want to make this small app threaded. The problem here is that I'm kind of blank of how to implement threading in my program.
This is my code which is a method which does the actual checking, the other part of the code is a simple button with for loop for advancing my port number.
I have read some basic threading tutorial but I just don't know how to implement this piece of code so I can check ports much faster.
private void ScanPort(IPAddress address, int port)
using (TcpClient client = new TcpClient())
IAsyncResult result = client.BeginConnect(address, port, null, null);
if (result.AsyncWaitHandle.WaitOne((int)nudTimeout.Value, false)) txtDisplay.AppendText("Port: " + port + " is open." + Environment.NewLine);
else txtDisplay.AppendText("Port: " + port + " is closed." + Environment.NewLine);
Hope for some advices.