Hi everybody.
I'm trying to develop e network scanner. I've run into trouble when got to the part of scanning the IP's :) ... meaning that I need to make the scanning method non-blocking. To do so I thought the Ping.SendAsync would come in handy. I've followed this example: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx but when I try to use it in a Winforms application the WaitOne() method blocks the PingCompletedCallback method that handles the pinging. I would really appreciate some help.
6 17969
What error are you receiving?
Your link points to .net3.0 (be sure you have .net3.0 or follow over to link relative to your own version of .net)
Why are you using the WaitOne() call? It's a blocking call. Seems like that would defeat the purpose of your async call?
I just did this, it will have run conditions, but works, -
Ping pi = new Ping();
-
private string allofit = "";
-
byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
-
-
void Form1_Load(object sender, EventArgs e)
-
{
-
pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
-
int i = 1;
-
pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
-
}
-
void pi_PingCompleted(object sender, PingCompletedEventArgs e)
-
{
-
PingReply pr = e.Reply;
-
allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
-
int i = (int)e.UserState;
-
i=i+1;
-
if (i < 10)
-
{
-
pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
-
}
-
else
-
{
-
MessageBox.Show(allofit);
-
}
-
//throw new Exception("The method or operation is not implemented.");
-
}
-
and a few secs later a window popping up telling me:
192.168.1 {success?]
192.168.2 {success?]
192.168.3 {success?]
192.168.4 {success?]
192.168.5 {success?]
192.168.6 {success?]
192.168.7 {success?]
192.168.8 {success?]
192.168.9 {success?]
I just did this, it will have run conditions, but works, -
Ping pi = new Ping();
-
private string allofit = "";
-
byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
-
-
void Form1_Load(object sender, EventArgs e)
-
{
-
pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
-
int i = 1;
-
pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
-
}
-
void pi_PingCompleted(object sender, PingCompletedEventArgs e)
-
{
-
PingReply pr = e.Reply;
-
allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
-
int i = (int)e.UserState;
-
i=i+1;
-
if (i < 10)
-
{
-
pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
-
}
-
else
-
{
-
MessageBox.Show(allofit);
-
}
-
//throw new Exception("The method or operation is not implemented.");
-
}
-
and a few secs later a window popping up telling me:
192.168.1 {success?]
192.168.2 {success?]
192.168.3 {success?]
192.168.4 {success?]
192.168.5 {success?]
192.168.6 {success?]
192.168.7 {success?]
192.168.8 {success?]
192.168.9 {success?]
Your code works but the behaviour I'm looking for is kinda different.
It's something like this: say I'm pinging a range of IP's starting with192.168.1.1 in the main thread using a for loop. At first the PingCompletedCallback returns the ping statistics from 192.168.1.1; then the main thread pings 192.168.1.2. At this point the PingCompletedCallback returns the ping statistics from another host, something like 192.168.1.32 or some other IP from the given range instead of the statistics for 192.168.1.2 and so on when the main thread gets to the next host. The point is that PingCompletedCallback pings all the IP's but not in the order that the main thread tells it to. And I figured that WaitOne does just that - it forces PingCompletedCallback to return the ping statistics from the current IP in the main thread. Or am I missing something? Or maybe I need another approach? I need the asynchronous ping so that the UI does not freeze while pinging the hosts.
Thanks for all your posts, guys.
Your code works but the behaviour I'm looking for is kinda different.
It's something like this: say I'm pinging a range of IP's starting with192.168.1.1 in the main thread using a for loop. At first the PingCompletedCallback returns the ping statistics from 192.168.1.1; then the main thread pings 192.168.1.2. At this point the PingCompletedCallback returns the ping statistics from another host, something like 192.168.1.32 or some other IP from the given range instead of the statistics for 192.168.1.2 and so on when the main thread gets to the next host. The point is that PingCompletedCallback pings all the IP's but not in the order that the main thread tells it to. And I figured that WaitOne does just that - it forces PingCompletedCallback to return the ping statistics from the current IP in the main thread. Or am I missing something? Or maybe I need another approach? I need the asynchronous ping so that the UI does not freeze while pinging the hosts.
Thanks for all your posts, guys.
You're basically forcing the SendAsync to act like the normal Send. If you need them all to come back in the specific order, then you'll have to block and use Send. So do it in another thread to avoid freezing the UI thread.
Something like
//in a button press event or something
new Thread (new ThreadStart(PingAll)).Start();
//now to create the PingAll method
private void PingAll()
{
//Do your Ping.Sends one at a time here
}
If you need to interact with the UI thread once the pinging is done, then you might want to look at the tutorial here
http://www.yoda.arachsys.com/csharp/threads/index.shtml
You can jump ahead to the section on Forms if you want, but it might be a bit confusing.
Have fun,
-mwalts
You're basically forcing the SendAsync to act like the normal Send. If you need them all to come back in the specific order, then you'll have to block and use Send. So do it in another thread to avoid freezing the UI thread.
Something like
//in a button press event or something
new Thread (new ThreadStart(PingAll)).Start();
//now to create the PingAll method
private void PingAll()
{
//Do your Ping.Sends one at a time here
}
If you need to interact with the UI thread once the pinging is done, then you might want to look at the tutorial here
http://www.yoda.arachsys.com/csharp/threads/index.shtml
You can jump ahead to the section on Forms if you want, but it might be a bit confusing.
Have fun,
-mwalts
Thank you very much, mwalts. Your link proved to be very useful. Thank you again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jason Rodman |
last post by:
I have downloaded every example on how to create a ping utility in .Net in
both VB and C#, but have been disappointed with the results. I have YET to
find an example that returns consistent results...
|
by: Ed |
last post by:
I've attached some VB.NET code I've hacked together (some taken from
MS examples & newsgroup postings) that will perform a ping or
IcmpSendEcho using the icmp.dll (see this for more info:...
|
by: scotty |
last post by:
I need to do a ping scan of a subnet.
I can Enum through the IPs and do this, but it takes over 5 minutes as
I can only create 1 process at a time. Does anyone know how I can
create multiple Ping...
|
by: Neel |
last post by:
I am trying to "ping" a remote host in my C++/Redhat Linux code to
check whether that host is connected or not.
if (0 == system("ping -w 2 192.168.0.2))
But, in both cases...
|
by: Deepak |
last post by:
I am programing a ping application which pings various centers . I
used timer loop and it pings one by one.
Now when i finish pinging one center it should wait for the
ping_completed function to...
|
by: Krish |
last post by:
All,
I have an offline application that works online for some data
syncronization. For data syncronization I access a webservice. I want
to show whether my application is online or not by checking...
|
by: Sid Price |
last post by:
I am trying to get Ping.SendAsync working and I am so far not having much
success. The completion event handler added in the following code is not
getting called. Can anyone spot what we are doing...
|
by: Dave Marden |
last post by:
I currently use this routine in vbscript to ping computers and get the
status of ping to determine whether to try to backup a machine, I am trying
to change it to work with vb2003.net I am...
|
by: Olivier MATROT |
last post by:
Hello,
I may be wrong, but if you are working with SmtpClient.SendAsync in ASP.NET
2.0 and it throws an exception, the thread processing the request waits
indefinitely for the operation to...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |